Python笔记-requests获取web数据及下载文件
生活随笔
收集整理的這篇文章主要介紹了
Python笔记-requests获取web数据及下载文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用Python去搞web獲取數(shù)據(jù)相關(guān)的東西的確方便。302重定向也能自動(dòng)解決。
如下FIddler抓包:
這個(gè)還是挺好的,可以直接處理302,掛證書也方便:
獲取數(shù)據(jù)源碼如下:
def getContent(self, path):if os.path.exists(path):with open(path, "r", encoding="utf-8") as f:return f.read()return ''下載文件源碼如下:
def downLoadFile(self, url, fileName):# r = requests.get(url)#r = requests.get(url, vertify = False)print("開始下載" + fileName)r = requests.get(url, proxies = {"http": "http://127.0.0.1:8888", "https": "http:127.0.0.1:8888"},verify=r"D:/Fiddler/FiddlerRoot.pem")print(fileName + "下載結(jié)束")if r.status_code == 200:#200直接下載with open(fileName, "wb") as code:code.write(r.content)passpass?
總結(jié)
以上是生活随笔為你收集整理的Python笔记-requests获取web数据及下载文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: QML笔记-对QML中信号与槽的进一步认
- 下一篇: Java笔记-使用RabbitMQ的Ja