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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

python

[python爬虫之路dya3]: requests库的基本使用

發(fā)布時(shí)間:2023/12/20 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [python爬虫之路dya3]: requests库的基本使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前面我們學(xué)習(xí)了urllib庫(kù)進(jìn)行源代碼的爬取,今天來(lái)介紹更加人性化的requests庫(kù)的使用。

import requests '''response=requests.get("https://baidu.com/") print(response.text)#獲取源代碼方法1 print(response.content.decode("utf-8"))#獲取源代碼方法2

****注意:****response.content是直接爬取的源代碼沒(méi)有經(jīng)過(guò)解碼的代碼,故是bytes類型,而response.text則是經(jīng)過(guò)requets庫(kù)自己的猜測(cè)進(jìn)行的解碼,有時(shí)可出現(xiàn)亂碼,所以此時(shí)應(yīng)該用response.content.decode(“utf-8”)
基本屬性:

print(response.url) #查看url地址 print(response.encoding) #查看響應(yīng)頭部字符編碼 print(response.status_code) #查看響應(yīng)碼

‘’’
#get請(qǐng)求用params, post請(qǐng)求用data
注意:可以如此使用請(qǐng)求:
1.requests.get(“https://baidu.com/”)
2.requests.post(“https://baidu.com/”)
例子1:對(duì)于“百度一下#深圳#”的源代碼(get請(qǐng)求實(shí)例):

params={"wd":"深圳"} headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"} ****response=requests.get("https://baidu.com/s",params=params,headers=headers)**** with open("baidu.html","w",encoding='utf-8')as fp:#保存到當(dāng)?shù)匚募?/span>fp.write(response.content.decode("utf-8")) print(response.url)

例子2:對(duì)于“拉鉤網(wǎng)#python#”的源代碼(get請(qǐng)求實(shí)例)

import requests data={"first": "true" , "pn": "1" , "kd": "python" } headers={"Referer":" https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"} response=requests.post("https://www.lagou.com/jobs/positionAjax.json?city=%E6%B7%B1%E5%9C%B3&needAddtionalResult=false",data=data,headers=headers) print(response.json)****#response.json可以將json源代碼轉(zhuǎn)換成字典或者列表的形式。**** *

requests庫(kù)使用代理IP*

import requests proxy={"HTTP":"114.226.246.144:9999"} response=requests.get("http://httpbin.org/ip",proxies=proxy) print(response.text)

#此處本人在實(shí)現(xiàn)時(shí)發(fā)現(xiàn)一直無(wú)法成功代理,在確保代碼無(wú)誤之后覺(jué)得是使用免費(fèi)代理的緣故,所以此處應(yīng)多次嘗試更換代理IP或者使用有償ip
requests處理cookie信息

import requests response=requests.get("https://baidu.com/") print(response.cookies)#*可通過(guò)這樣獲取cookie信息* print(response.cookies.get_dict())#*具體的cookie(字典形式)*

**如果在多次使用中共享cookie信息,那么應(yīng)該使用session**

url="http://www.renren.com/" data={"email":"135*********","password":"***08***"} headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36"} session=requests.session() session.post(url,data=data,headers=headers) response=session.get("http://www.renren.com/973687886/profile") with open("renren.html","w",encoding="utf-8") as fp:fp.write(response.text)`

處理不信任的SSL證書:(爬取某些網(wǎng)頁(yè)無(wú)法進(jìn)入時(shí))

response=requests.get("http://*******",verify=False) print(response.content.decode("utf-8"))

```

總結(jié)

以上是生活随笔為你收集整理的[python爬虫之路dya3]: requests库的基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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