requests庫比urllib庫更加方便,包含了很多功能。
1、在使用之前需要先安裝pip,在pycharm中打開:
寫入pip install requests命令,即可下載
在github中有關於requests庫的介紹,網址:https://github.com/requests/requests
2、Get請求
response=requests.get("https://www.baidu.com/")
我們要完成在百度的頁面獲取中國的相關信息,相當於
輸入中國:
用爬蟲代碼實驗實現:
import requests # wd是在網址中后面的一段 params={ 'wd':'中國' } headers={ 'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" } # 這時我們要在這個網址中加入S response=requests.get("http://www.baidu.com/s",params=params,headers=headers) with open('baidu.html','w',encoding='utf-8') as fp: fp.write(response.content.decode('utf-8'))
打開后就是中國的相關信息:
3、response.txt和response.content的區別
response.txt是 requests是經response.content解碼的字符串,requests會根據自己的猜測來進行解碼,有時候會猜測錯誤,導致亂碼。
response.content是直接從網上爬取的數據,沒有經過經過任何解碼,是bytes類型。
所以最常用的就是:response.content.decode('utf-8')