日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python爬虫---requests库的用法

發(fā)布時(shí)間:2024/9/20 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫---requests库的用法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

requests是python實(shí)現(xiàn)的簡(jiǎn)單易用的HTTP庫(kù),使用起來(lái)比urllib簡(jiǎn)潔很多

因?yàn)槭堑谌綆?kù),所以使用前需要cmd安裝

pip install requests

安裝完成后import一下,正常則說(shuō)明可以開始使用了。

基本用法:

requests.get()用于請(qǐng)求目標(biāo)網(wǎng)站,類型是一個(gè)HTTPresponse類型

import requestsresponse = requests.get('http://www.baidu.com') print(response.status_code) # 打印狀態(tài)碼 print(response.url) # 打印請(qǐng)求url print(response.headers) # 打印頭信息 print(response.cookies) # 打印cookie信息 print(response.text) #以文本形式打印網(wǎng)頁(yè)源碼 print(response.content) #以字節(jié)流形式打印

運(yùn)行結(jié)果:

狀態(tài)碼:200

url:www.baidu.com

headers信息

?

?

?各種請(qǐng)求方式:

import requestsrequests.get('http://httpbin.org/get') requests.post('http://httpbin.org/post') requests.put('http://httpbin.org/put') requests.delete('http://httpbin.org/delete') requests.head('http://httpbin.org/get') requests.options('http://httpbin.org/get')

?

基本的get請(qǐng)求

import requestsresponse = requests.get('http://httpbin.org/get') print(response.text)

結(jié)果

?

?

?

帶參數(shù)的GET請(qǐng)求:

第一種直接將參數(shù)放在url內(nèi)

import requestsresponse = requests.get(http://httpbin.org/get?name=gemey&age=22) print(response.text)

結(jié)果

另一種先將參數(shù)填寫在dict中,發(fā)起請(qǐng)求時(shí)params參數(shù)指定為dict

import requestsdata = {'name': 'tom','age': 20 }response = requests.get('http://httpbin.org/get', params=data) print(response.text)

結(jié)果同上

?

解析json

import requestsresponse = requests.get('http://httpbin.org/get') print(response.text) print(response.json()) #response.json()方法同json.loads(response.text) print(type(response.json()))

結(jié)果

?

簡(jiǎn)單保存一個(gè)二進(jìn)制文件

二進(jìn)制內(nèi)容為response.content

import requestsresponse = requests.get('http://img.ivsky.com/img/tupian/pre/201708/30/kekeersitao-002.jpg') b = response.content with open('F://fengjing.jpg','wb') as f:f.write(b)

?

為你的請(qǐng)求添加頭信息

import requests heads = {} heads['User-Agent'] = 'Mozilla/5.0 ' \'(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ' \'(KHTML, like Gecko) Version/5.1 Safari/534.50' response = requests.get('http://www.baidu.com',headers=headers)

?

使用代理

同添加headers方法,代理參數(shù)也要是一個(gè)dict

這里使用requests庫(kù)爬取了IP代理網(wǎng)站的IP與端口和類型

因?yàn)槭敲赓M(fèi)的,使用的代理地址很快就失效了。

import requests import redef get_html(url):proxy = {'http': '120.25.253.234:812','https' '163.125.222.244:8123'}heads = {}heads['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'req = requests.get(url, headers=heads,proxies=proxy)html = req.textreturn htmldef get_ipport(html):regex = r'<td data-title="IP">(.+)</td>'iplist = re.findall(regex, html)regex2 = '<td data-title="PORT">(.+)</td>'portlist = re.findall(regex2, html)regex3 = r'<td data-title="類型">(.+)</td>'typelist = re.findall(regex3, html)sumray = []for i in iplist:for p in portlist:for t in typelist:passpassa = t+','+i + ':' + psumray.append(a)print('高匿代理')print(sumray)if __name__ == '__main__':url = 'http://www.kuaidaili.com/free/'get_ipport(get_html(url))

結(jié)果:

?

?

基本POST請(qǐng)求:

import requestsdata = {'name':'tom','age':'22'}response = requests.post('http://httpbin.org/post', data=data)

?

?獲取cookie

#獲取cookie import requestsresponse = requests.get('http://www.baidu.com') print(response.cookies) print(type(response.cookies)) for k,v in response.cookies.items():print(k+':'+v)

結(jié)果:

?

?

會(huì)話維持

import requestssession = requests.Session() session.get('http://httpbin.org/cookies/set/number/12345') response = session.get('http://httpbin.org/cookies') print(response.text)

結(jié)果:

?

證書驗(yàn)證設(shè)置

import requests from requests.packages import urllib3urllib3.disable_warnings() #從urllib3中消除警告 response = requests.get('https://www.12306.cn',verify=False) #證書驗(yàn)證設(shè)為FALSE print(response.status_code)打印結(jié)果:200

?

超時(shí)異常捕獲

import requests from requests.exceptions import ReadTimeouttry:res = requests.get('http://httpbin.org', timeout=0.1)print(res.status_code) except ReadTimeout:print(timeout)

?

異常處理

在你不確定會(huì)發(fā)生什么錯(cuò)誤時(shí),盡量使用try...except來(lái)捕獲異常

所有的requests exception:

Exceptions

import requests from requests.exceptions import ReadTimeout,HTTPError,RequestExceptiontry:response = requests.get('http://www.baidu.com',timeout=0.5)print(response.status_code) except ReadTimeout:print('timeout') except HTTPError:print('httperror') except RequestException:print('reqerror')

來(lái)源:https://www.cnblogs.com/mzc1997/p/7813801.html

總結(jié)

以上是生活随笔為你收集整理的python爬虫---requests库的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。