生活随笔
收集整理的這篇文章主要介紹了
python爬虫程序requests采用get和post方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
第1個爬蟲程序:
main.py
from urllib
.request
import urlopen
url
= "https://www.hao123.com/"if __name__
== '__main__':resp
= urlopen
(url
)with open("myGetFile.html", mode
="w", encoding
='utf-8') as f
:f
.write
(resp
.read
().decode
('utf-8'))f
.close
() resp
.close
() print("結(jié)束")
第2個爬蟲程序:
main2.py
import requests
if __name__
== '__main__':query
= input("請輸入一個你喜歡的明星:")url
= f"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&srcqid=5655130659909863611&tn=50000021_hao_pg&wd={query}"dic
= {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ""Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47"}resp
= requests
.get
(url
, headers
=dic
) with open("myGetFile.html", mode
="w", encoding
='utf-8') as f
:f
.write
(resp
.text
) f
.close
() resp
.close
() print("結(jié)束")
第3個爬蟲程序:
main3.py
import requests
if __name__
== '__main__':url
= "https://fanyi.baidu.com/sug"s
= input("請輸入你要翻譯的英文單詞:")dat
= {"kw": s
}dicHeaders
= {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ""Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47"}resp
= requests
.post
(url
, headers
=dicHeaders
, data
=dat
)print(resp
.json
())resp
.close
() print("結(jié)束")
第4個爬蟲程序:
main4.py
import requests
if __name__
== '__main__':url
= "https://movie.douban.com/j/chart/top_list"dicParam
= {"type": "24","interval_id": "100:90","action": "","start": "0","limit": "20"}dicHeaders
= {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ""Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47"}resp
= requests
.get
(url
=url
, params
=dicParam
, headers
=dicHeaders
) objData
= resp
.json
()for i
in range(0, len(objData
)):print(objData
[i
])resp
.close
() print("結(jié)束")
第5個爬蟲程序:
main5.py
import requests
if __name__
== '__main__':url
= "https://movie.douban.com/j/chart/top_list"for n
in range(0, 2):dicParam
= {"type": "24","interval_id": "100:90","action": "","start": f"{20*n}","limit": "20"}dicHeaders
= {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ""Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47"}resp
= requests
.get
(url
=url
, params
=dicParam
, headers
=dicHeaders
) objData
= resp
.json
()print(f"第{n+1}輪20個數(shù)據(jù):")for i
in range(0, len(objData
)):print(objData
[i
])resp
.close
() print("結(jié)束")
關(guān)注公眾號,獲取更多資料
總結(jié)
以上是生活随笔為你收集整理的python爬虫程序requests采用get和post方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。