Python Requests爬蟲之求取關鍵詞頁面詳解
更新時間:2022年02月16日 10:10:18 作者:那人獨釣寒江雪.
這篇文章主要為大家詳細介紹了Python Requests爬蟲之求取關鍵詞頁面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
需求:爬取搜狗首頁的頁面數(shù)據(jù)
import requestsif __name__=='__main__': #step 1:搜索Url url='https://123.sogou.com/' #step 2:發(fā)起請求 #get方法會返回一個響應對象 response=requests.get(url=url) #step 3:獲取響應數(shù)據(jù),text返回的是字符串形式的響應數(shù)據(jù) page_text=response.text print(page_text) #step 4:持久化存儲 with open('./sogou.html','w',encoding='utf-8') as fp: fp.write(page_text) print("爬取數(shù)據(jù)結束")import requests if __name__=='__main__': #step 1:搜索Url url='https://123.sogou.com/' #step 2:發(fā)起請求 #get方法會返回一個響應對象 response=requests.get(url=url) #step 3:獲取響應數(shù)據(jù),text返回的是字符串形式的響應數(shù)據(jù) page_text=response.text print(page_text) #step 4:持久化存儲 with open('./sogou.html','w',encoding='utf-8') as fp: fp.write(page_text) print("爬取數(shù)據(jù)結束")
使用UA偽裝 求取關鍵詞頁面
import requests if __name__=='__main__': #UA偽裝:將對應的User-Agent封裝到一個字典中 headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.9 Safari/537.36' } url='https://www.sogou.com/sie?' #處理url攜帶的參數(shù):封裝到字典中 kw=input('enter a word:') param={ 'query':kw } #對指定的url發(fā)起的請求對應的url是攜帶參數(shù)的,并且請求過程中處理了參數(shù) response=requests.get(url=url,params=param,headers=headers)#headers是偽裝 params輸入關鍵詞 page_text=response.text#以文本的形式輸出 fileName=kw+'.html'#存儲為網(wǎng)頁形式 with open(fileName,'w+',encoding='utf-8') as fp: fp.write(page_text)#寫入fp print(fileName,"保存成功??!")
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
python web.py開發(fā)httpserver解決跨域問題實例解析
這篇文章主要介紹了python web.py開發(fā)httpserver解決跨域問題實例解析,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02mac 安裝python網(wǎng)絡請求包requests方法
今天小編就為大家分享一篇mac 安裝python網(wǎng)絡請求包requests方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Pygame游戲開發(fā)之太空射擊實戰(zhàn)盾牌篇
相信大多數(shù)8090后都玩過太空射擊游戲,在過去游戲不多的年代太空射擊自然屬于經(jīng)典好玩的一款了,今天我們來自己動手實現(xiàn)它,在編寫學習中回顧過往展望未來,在本課中,我們將為玩家添加一個盾牌以及一個用于顯示盾牌等級的欄2022-08-08