首頁 > 後端開發 > Python教學 > Python利用Requests庫寫爬蟲的實例詳解

Python利用Requests庫寫爬蟲的實例詳解

零下一度
發布: 2017-06-30 18:00:56
原創
2709 人瀏覽過

基本Get請求:

#-*- coding:utf-8 -*-import requests
url = 'www.baidu.com'r = requests.get(url)print r.text
登入後複製

帶參數Get請求:

#-*- coding:utf-8 -*-import requests
url = 'http://www.baidu.com'payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get(url, params=payload)print r.text
登入後複製

POST請求模擬登陸及一些回傳物件的方法:

#-*- coding:utf-8 -*-import requests
url1 = 'www.exanple.com/login'#登陆地址url2 = "www.example.com/main"#需要登陆才能访问的地址data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",            "Accept-Encoding":"gzip",            "Accept-Language":"zh-CN,zh;q=0.8",            "Referer":"www.example.com/",            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
            }
res1 = requests.post(url1, data=data, headers=headers)
res2 = requests.get(url2, cookies=res1.cookies, headers=headers)print res2.conten
登入後複製

t#取得二進位回應內容print res2.raw#取得原始回應內容,需要stream=Trueprint res2.raw.read(50)print type(res2.text)#回傳解碼成unicode的內容print res2.urlprint res2.history#追蹤重定向print res2. cookiesprint res2.cookies['example_cookie_name']print res2.headersprint res2.headers['Content-Type']print res2.headers.get('content-type')print res2.json#講回內容編碼為jsonprint res2.encoding #返回內容編碼print res2.status_code#傳回http狀態碼print res2.raise_for_status()#傳回錯誤狀態碼

使用Session()物件的寫法(Prepared Requests):

#-*- coding:utf-8 -*-import requests
s = requests.Session()
url1 = 'www.exanple.com/login'#登陆地址url2 = "www.example.com/main"#需要登陆才能访问的地址data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",            "Accept-Encoding":"gzip",            "Accept-Language":"zh-CN,zh;q=0.8",            "Referer":"http://www.example.com/",            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
            }
prepped1 = requests.Request('POST', url1,
    data=data,
    headers=headers
).prepare()
s.send(prepped1)'''
也可以这样写
res = requests.Request('POST', url1,
data=data,
headers=headers
)
prepared = s.prepare_request(res)
# do something with prepped.body
# do something with prepped.headers
s.send(prepared)
'''prepare2 = requests.Request('POST', url2,
    headers=headers
).prepare()
res2 = s.send(prepare2)print res2.content
登入後複製

另一種寫法:

#-*- coding:utf-8 -*-import requestss = requests.Session()url1 = 'www.exanple.com/login'#登陆地址url2 = "www.example.com/main"#需要登陆才能访问的页面地址data={"user":"user","password":"pass"}headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",            "Accept-Encoding":"gzip",            "Accept-Language":"zh-CN,zh;q=0.8",            "Referer":"http://www.example.com/",            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"            }res1 = s.post(url1, data=data)res2 = s.post(url2)print(resp2.content)
SessionApi
其他的一些请求方式
>>> r = requests.put("http://httpbin.org/put")>>> r = requests.delete("http://httpbin.org/delete")>>> r = requests.head("http://httpbin.org/get")>>> r = requests.options("http://httpbin.org/get")
登入後複製

遇到的問題:

在cmd下執行,遇到小錯誤:

UnicodeEncodeError:'gbk' codec can' t encode character u'\xbb' in   position 23460: illegal multibyte sequence

分析:
1、Unicode是編碼還是解碼

UnicodeEncodeError

##很明顯是在編碼的時候出現了錯誤

2、用了什麼編碼

'gbk' codec can't encode character


使用GBK編碼出錯

解決方法:

確定目前字串,例如

#-*- coding:utf-8 -*-import requests
url = 'www.baidu.com'r = requests.get(url)print r.encoding
>utf-8
登入後複製
已經確定html的字串是utf-8的,則可以直接去通過utf-8去編碼。

print r.text.encode('utf-8')



#

以上是Python利用Requests庫寫爬蟲的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板