python爬蟲(chóng) 基于requests模塊的get請(qǐng)求實(shí)現(xiàn)詳解
需求:爬取搜狗首頁(yè)的頁(yè)面數(shù)據(jù)
import requests # 1.指定url url = 'https://www.sogou.com/' # 2.發(fā)起get請(qǐng)求:get方法會(huì)返回請(qǐng)求成功的響應(yīng)對(duì)象 response = requests.get(url=url) # 3.獲取響應(yīng)中的數(shù)據(jù):text屬性作用是可以獲取響應(yīng)對(duì)象中字符串形式的頁(yè)面數(shù)據(jù) page_data = response.text # 4.持久化數(shù)據(jù) with open("sougou.html","w",encoding="utf-8") as f: f.write(page_data) f.close() print("ok")
requests模塊如何處理攜帶參數(shù)的get請(qǐng)求,返回?cái)y帶參數(shù)的請(qǐng)求
需求:指定一個(gè)詞條,獲取搜狗搜索結(jié)果所對(duì)應(yīng)的頁(yè)面數(shù)據(jù)
之前urllib模塊處理url上參數(shù)有中文的需要處理編碼,requests會(huì)自動(dòng)處理url編碼
發(fā)起帶參數(shù)的get請(qǐng)求
params可以是傳字典或者列表
def get(url, params=None, **kwargs): r"""Sends a GET request. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary, list of tuples or bytes to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :return: :class:`Response <Response>` object :rtype: requests.Response
import requests # 指定url url = 'https://www.sogou.com/web' # 封裝get請(qǐng)求參數(shù) prams = { 'query':'周杰倫', 'ie':'utf-8' } response = requests.get(url=url,params=prams) page_text = response.text with open("周杰倫.html","w",encoding="utf-8") as f: f.write(page_text) f.close() print("ok")
利用requests模塊自定義請(qǐng)求頭信息,并且發(fā)起帶參數(shù)的get請(qǐng)求
get方法有個(gè)headers參數(shù) 把請(qǐng)求頭信息的字典賦給headers參數(shù)
import requests # 指定url url = 'https://www.sogou.com/web' # 封裝get請(qǐng)求參數(shù) prams = { 'query':'周杰倫', 'ie':'utf-8' } # 自定義請(qǐng)求頭信息 headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', } response = requests.get(url=url,params=prams,headers=headers) page_text = response.text with open("周杰倫.html","w",encoding="utf-8") as f: f.write(page_text) f.close() print("ok")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch中Tensor.to(device)和model.to(device)的區(qū)別及說(shuō)明
這篇文章主要介紹了pytorch中Tensor.to(device)和model.to(device)的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07python+opencv邊緣提取與各函數(shù)參數(shù)解析
這篇文章主要介紹了python+opencv邊緣提取與各函數(shù)參數(shù)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03淺談pytorch torch.backends.cudnn設(shè)置作用
今天小編就為大家分享一篇淺談pytorch torch.backends.cudnn設(shè)置作用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02pandas 按照特定順序輸出的實(shí)現(xiàn)代碼
這篇文章主要介紹了pandas 按照特定順序輸出的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07Python報(bào)錯(cuò):NameError:?name?‘xxx‘?is?not?defined的解決辦法
這篇文章主要給大家介紹了關(guān)于Python報(bào)錯(cuò):NameError:?name?‘xxx‘?is?not?defined的解決辦法,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06Python正則表達(dá)式字符串的匹配、替換、分割、查找方式
這篇文章主要介紹了Python正則表達(dá)式字符串的匹配、替換、分割、查找方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07