最近在寫一些爬蟲相關的小項目,了解了一下request模塊的get()方法
這里記錄一下,request.get的常用參數
1、設置proxy代理及user_agent兩個參數
import requests from lxml import etree user_agent = {'User-agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"} # 測試https需要有,用於訪問https的網站需要的 proxies={ 'http':'proxy.qq_5201351.com.cn:8080', 'https':'proxy.qq_5201351.com.cn:8080' } # 如果不需要代理,在get方法中可以不寫 headers = user_agent,也可以使用如下方式定義為空字典{} # proxies={} response=requests.get("https://www.cnblogs.com/5201351",headers = user_agent,proxies=proxies) text=response.text
另:一般在得到response.text,如果需要使用xpath去解析,可以導入lxml模塊的etree
尊重別人的勞動成果 轉載請務必注明出處:https://www.cnblogs.com/5201351/p/15583042.html