Python網(wǎng)絡(luò)爬蟲(chóng)信息提取mooc代碼實(shí)例
實(shí)例一--爬取頁(yè)面
import requests url="https//itemjd.com/2646846.html" try: r=requests.get(url) r.raise_for_status() r.encoding=r.apparent_encoding print(r.text[:1000]) except: print("爬取失敗")
正常頁(yè)面爬取
實(shí)例二--爬取頁(yè)面
import requests url="https://www.amazon.cn/gp/product/B01M8L5Z3Y" try: kv={'user-agent':'Mozilla/5.0'} r=requests.get(url,headers=kv) r.raise_for_status() r.encoding=r.apparent_encoding print(r.text[1000:2000]) except: print("爬取失敗")
對(duì)訪(fǎng)問(wèn)用戶(hù)名有限制,模擬瀏覽器對(duì)網(wǎng)站請(qǐng)求
實(shí)例三--爬取搜索引擎
#百度的關(guān)鍵詞接口:http://www.baidu.com/s?wd=keyword #360的關(guān)鍵詞接口:http://www.so.com/s?q=keyword import requests keyword="python" try: kv={'wd':keyword} r=requests.get("http://www.baidu.com/s",params=kv) print(r.request.url) r.raise_for_status() print(len(r.text)) except: print("爬取失敗") -------------------------------------------------- import requests keyword="python" try: kv={'q':keyword} r=requests.get("http://www.so.com/s",params=kv) print(r.request.url) r.raise_for_status() print(len(r.text)) except: print("爬取失敗")
實(shí)例四--:爬取圖片
import requests import os url="http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg" root="F://pics//" path=root+url.split('/')[-1] try: if not os.path.exists(root): os.mkdir(root) if not os.path.exists(path): r=requests.get(url) with open(path,'wb') as f: f.write(r.content) f.close() print("文件保存成功") else: print("文件已經(jīng)存在") except: print("爬取失敗")
爬取并保存圖片
實(shí)例五--IP地址歸屬地查詢(xún):
http://m.ip138.com/ip.asp?ip=ipaddress
url="http://www.ip138.com/iplookup.asp?ip=" try: r=requests.get(url+'202.204.80.112'+'&action=2') r.raise_for_status() r.encoding=r.apparent_encoding print(r.text[-500:]) except: print("爬取失敗")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python 日志模塊logging的使用場(chǎng)景及示例
- Python中l(wèi)ogging日志的四個(gè)等級(jí)和使用
- python tkinter的消息框模塊(messagebox,simpledialog)
- python讀取raw binary圖片并提取統(tǒng)計(jì)信息的實(shí)例
- python 利用正則表達(dá)式提取特殊信息
- python提取照片坐標(biāo)信息的實(shí)例代碼
- Python網(wǎng)絡(luò)爬蟲(chóng)與信息提取(實(shí)例講解)
- python批量提取word內(nèi)信息
- 如何用Python提取10000份log中的產(chǎn)品信息
相關(guān)文章
淺談OpenCV中的新函數(shù)connectedComponentsWithStats用法
這篇文章主要介紹了淺談OpenCV中的新函數(shù)connectedComponentsWithStats用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07Python實(shí)現(xiàn)的一個(gè)自動(dòng)售飲料程序代碼分享
這篇文章主要介紹了Python實(shí)現(xiàn)的一個(gè)自動(dòng)售飲料程序代碼分享,就是用python實(shí)現(xiàn)的生活中一種投幣式自動(dòng)售飲料機(jī)的內(nèi)部程序判斷代碼,需要的朋友可以參考下2014-08-08python卸載numpy出現(xiàn)WinError:拒絕訪(fǎng)問(wèn)的解決方案
這篇文章主要介紹了python卸載numpy出現(xiàn)WinError:拒絕訪(fǎng)問(wèn)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Win10操作系統(tǒng)中PyTorch虛擬環(huán)境配置+PyCharm配置
本文主要介紹了Win10操作系統(tǒng)中PyTorch虛擬環(huán)境配置+PyCharm配置,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09python ftfy庫(kù)處理金融方面文件編碼錯(cuò)誤實(shí)例詳解
這篇文章主要為大家介紹了使用python ftfy庫(kù)處理金融方面文件編碼錯(cuò)誤實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01使用Python腳本生成隨機(jī)IP的簡(jiǎn)單方法
這篇文章主要介紹了使用Python腳本生成隨機(jī)IP的簡(jiǎn)單方法,并且可以自己設(shè)定IP數(shù)值范圍,需要的朋友可以參考下2015-07-07python 寫(xiě)入csv亂碼問(wèn)題解決方法
這篇文章主要介紹了python 寫(xiě)入csv亂碼問(wèn)題解決方法的相關(guān)資料,需要的朋友可以參考下2016-10-10