Python網(wǎng)絡(luò)爬蟲信息提取mooc代碼實(shí)例
實(shí)例一--爬取頁面
import requestsurl='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('爬取失敗')
正常頁面爬取
實(shí)例二--爬取頁面
import requestsurl='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ì)訪問用戶名有限制,模擬瀏覽器對(duì)網(wǎng)站請(qǐng)求
實(shí)例三--爬取搜索引擎
#百度的關(guān)鍵詞接口:http://www.baidu.com/s?wd=keyword#360的關(guān)鍵詞接口:http://www.so.com/s?q=keywordimport requestskeyword='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 requestskeyword='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 requestsimport osurl='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地址歸屬地查詢:
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('爬取失敗')
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. css進(jìn)階學(xué)習(xí) 選擇符2. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式3. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法4. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法5. JSP之表單提交get和post的區(qū)別詳解及實(shí)例6. asp讀取xml文件和記數(shù)7. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能8. UDDI FAQs9. ASP常用日期格式化函數(shù) FormatDate()10. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))
