国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python制作簡單的網頁爬蟲

2019-11-25 17:01:28
字體:
來源:轉載
供稿:網友

1.準備工作:

工欲善其事必先利其器,因此我們有必要在進行Coding前先配置一個適合我們自己的開發環境,我搭建的開發環境是:

操作系統:Ubuntu 14.04 LTS
Python版本:2.7.6
代碼編輯器:Sublime Text 3.0

這次的網絡爬蟲需求背景我打算延續DotNet開源大本營在他的那篇文章中的需求,這里就不再詳解。我們只抓取某一省中所有主要城市從2015-11-22到2015-10-24的白天到夜間的所有天氣情況。這里以湖北省為例。
2.實戰網頁爬蟲:
2.1.獲取城市列表:
首先,我們需要獲取到湖北省所有城市的網頁,然后進行網頁解析。網絡地址為:http://www.tianqihoubao.com/weather/province.aspx?id=420000
我們查看該網頁的源碼可以發現所有的城市列表都是以<td style="height: 22px" align="center"><a href="城市天氣鏈接+城市名稱">,因此,我們可以封裝一個函數來通過使用正則表達式獲取我們想要的數據,示例代碼如下所示:

復制代碼 代碼如下:

抓取的結果如下所示:

 1 top/anlu.html" title="安陸歷史天氣查詢 2 top/badong.html" title="巴東歷史天氣查詢 3 top/baokang.html" title="保康歷史天氣查詢 4 top/caidian.html" title="蔡甸歷史天氣查詢 5 top/changyang.html" title="長陽歷史天氣查詢 6 top/chibi.html" title="赤壁歷史天氣查詢 7 top/chongyang.html" title="崇陽歷史天氣查詢 8 top/dawu.html" title="大悟歷史天氣查詢 9 top/daye.html" title="大冶歷史天氣查詢10 top/danjiangkou.html" title="丹江口歷史天氣查詢11 top/dangyang.html" title="當陽歷史天氣查詢12 top/ezhou.html" title="鄂州歷史天氣查詢13 top/enshi.html" title="恩施歷史天氣查詢14 top/fangxian.html" title="房縣歷史天氣查詢15 top/gongan.html" title="公安歷史天氣查詢16 top/gucheng.html" title="谷城歷史天氣查詢17 top/guangshui.html" title="廣水歷史天氣查詢18 top/hanchuan.html" title="漢川歷史天氣查詢19 top/hanyang.html" title="漢陽歷史天氣查詢20 top/hefeng.html" title="鶴峰歷史天氣查詢21 top/hongan.html" title="紅安歷史天氣查詢22 top/honghu.html" title="洪湖歷史天氣查詢23 top/huangpi.html" title="黃陂歷史天氣查詢24 top/huanggang.html" title="黃岡歷史天氣查詢25 top/huangmei.html" title="黃梅歷史天氣查詢26 top/huangshi.html" title="黃石歷史天氣查詢27 top/jiayu.html" title="嘉魚歷史天氣查詢28 top/jianli.html" title="監利歷史天氣查詢29 top/jianshi.html" title="建始歷史天氣查詢30 top/jiangxia.html" title="江夏歷史天氣查詢31 top/jingshan.html" title="京山歷史天氣查詢32 top/jingmen.html" title="荊門歷史天氣查詢33 top/jingzhou.html" title="荊州歷史天氣查詢34 top/laifeng.html" title="來鳳歷史天氣查詢35 top/laohekou.html" title="老河口歷史天氣查詢36 top/lichuan.html" title="利川歷史天氣查詢37 top/lvtian.html" title="羅田歷史天氣查詢38 top/macheng.html" title="麻城歷史天氣查詢39 top/nanzhang.html" title="南漳歷史天氣查詢40 top/qichun.html" title="蘄春歷史天氣查詢41 top/qianjiang.html" title="潛江歷史天氣查詢42 top/sanxia.html" title="三峽歷史天氣查詢43 top/shennongjia.html" title="神農架歷史天氣查詢44 top/shiyan.html" title="十堰歷史天氣查詢45 top/shishou.html" title="石首歷史天氣查詢46 top/songzi.html" title="松滋歷史天氣查詢47 top/suizhou.html" title="隨州歷史天氣查詢48 top/tianmen.html" title="天門歷史天氣查詢49 top/hbtongcheng.html" title="通城歷史天氣查詢50 top/tongshan.html" title="通山歷史天氣查詢51 top/wufeng.html" title="五峰歷史天氣查詢52 top/wuchang.html" title="武昌歷史天氣查詢53 top/wuhan.html" title="武漢歷史天氣查詢54 top/wuxue.html" title="武穴歷史天氣查詢55 top/hbxishui.html" title="浠水歷史天氣查詢56 top/xiantao.html" title="仙桃歷史天氣查詢57 top/xianfeng.html" title="咸豐歷史天氣查詢58 top/xianning.html" title="咸寧歷史天氣查詢59 top/xiangyang.html" title="襄陽歷史天氣查詢60 top/xiaogan.html" title="孝感歷史天氣查詢61 top/hbxinzhou.html" title="新洲歷史天氣查詢62 top/xingshan.html" title="興山歷史天氣查詢63 top/xuanen.html" title="宣恩歷史天氣查詢64 top/hbyangxin.html" title="陽新歷史天氣查詢65 top/yiling.html" title="夷陵歷史天氣查詢66 top/yichang.html" title="宜昌歷史天氣查詢67 top/yicheng.html" title="宜城歷史天氣查詢68 top/yidu.html" title="宜都歷史天氣查詢69 top/yingcheng.html" title="應城歷史天氣查詢70 top/hbyingshan.html" title="英山歷史天氣查詢71 top/yuanan.html" title="遠安歷史天氣查詢72 top/yunmeng.html" title="云夢歷史天氣查詢73 top/yunxi.html" title="鄖西歷史天氣查詢74 top/hbyunxian.html" title="鄖縣歷史天氣查詢75 top/zaoyang.html" title="棗陽歷史天氣查詢76 top/zhijiang.html" title="枝江歷史天氣查詢77 top/zhongxiang.html" title="鐘祥歷史天氣查詢78 top/zhushan.html" title="竹山歷史天氣查詢79 top/zhuxi.html" title="竹溪歷史天氣查詢80 top/zigui.html" title="秭歸歷史天氣查詢81 [Finished in 15.4s]


2.2.獲取對應城市的所有天氣信息:
然后我們需要根據抓取到的城市鏈接去抓取對應城市的天氣情況,這里我們再封裝一個函數用于顯示對應城市的所有天氣狀況:

def ShowWeather(city): res =str(city).split('" title="') print res[1],'(白天-->夜間)' html=requests.get("http://www.tianqihoubao.com/weather/{0}".format(res[0])) weather=re.search('<table width="100%" border="0" class="b" cellpadding="1" cellspacing="1">(.*?)</table>', html.text,re.S).group(1) res=re.findall('<tr>(.*?)</tr>', weather,re.S) for x in res[2:]:  w = re.findall('>(.*?)<', x,re.S)  for y in w[1:]:   if len(y.strip())<=0:    pass    else:     print y  print '--'*40

這樣以來,我們就可以獲取到了對應城市的天氣情況了!!

完整代碼:

#coding:UTF-8import reimport requestsimport sysreload(sys)sys.setdefaultencoding('UTF-8')def ShowWeather(city): res =str(city).split('" title="') print res[1],'(白天-->夜間)' html=requests.get("http://www.tianqihoubao.com/weather/{0}".format(res[0])) weather=re.search('<table width="100%" border="0" class="b" cellpadding="1" cellspacing="1">(.*?)</table>', html.text,re.S).group(1) res=re.findall('<tr>(.*?)</tr>', weather,re.S) for x in res[2:]:  w = re.findall('>(.*?)<', x,re.S)  for y in w[1:]:   if len(y.strip())<=0:    pass   else:    print y  print '--'*40 print '/n','*'*40def ShowCity(): html=requests.get("http://www.tianqihoubao.com/weather/province.aspx?id=420000") citys= re.findall('<td style="height: 22px" align="center"><a href="(.*?)">', html.text,re.S) for city in citys:  ShowWeather(city)def main(): ShowCity()if __name__=='__main__': main()

是的,你沒有看錯,短短34行代碼就可以爬取湖北省所有的主要城市1個月的所有天氣情況,是不是很厲害呀!!???不過不要高興的太早,凡事有利有弊,看看它的運行結果吧:[Finished in 371.8s]

3.知識總結:  

3.1.編碼問題: 
#在ubuntu上,由于編碼問題,我們需要在代碼的開始位置添加一行注釋,告訴Pyhton解釋器我們指定的編碼格式:

#此外,我們還需要設置默認的編碼格式,否則Sublime Text會無法識別中文,報告一個錯誤:“UnicodeEncodeError: 'ascii' codec can't encode characters in position”

#-*-coding:utf8-*-import sysreload(sys)sys.setdefaultencoding('UTF-8')

3.2.正則表達式:

導入正則表達式庫:import re
匹配任意字符:.
匹配前一個字符0次或無限次:*
匹配前一個字符0次或一次:?
貪心算法:.*
非貪心算法:.*?
匹配數字:(/d+)
常用函數:

re.findall(pattern, string)re.search(pattern, string)re.sub(pattern, repl, string)

最后的最后,如果你嘗試過運行我貼出來的完整代碼,或許你會遇到和我一樣的瓶頸,就是運行的速度不夠快(尤其像我這種機器配置不是很好的電腦)。在我的機器上運行這段腳本總共花費了 371.8s。我運行過多次,每次都是在350+。因此,如果你的程序不在乎運行速度,那么可能Python還是挺適合的,畢竟可以通過它寫更少的代碼去做更多的事情!!!!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阜新市| 鄯善县| 静宁县| 长宁县| 昆山市| 柳江县| 吉隆县| 县级市| 闻喜县| 宁武县| 吴旗县| 忻城县| 如皋市| 宁都县| 乌拉特前旗| 贵州省| 泉州市| 宜良县| 阳春市| 汝城县| 三亚市| 淳安县| 常州市| 拜泉县| 铁岭县| 化隆| 邛崃市| 盐亭县| 岢岚县| 佛冈县| 类乌齐县| 竹山县| 灵山县| 叙永县| 毕节市| 涟源市| 双牌县| 昌江| 平远县| 延津县| 调兵山市|