亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python制作簡(jiǎn)單的網(wǎng)頁(yè)爬蟲(chóng)

系統(tǒng) 1768 0

1.準(zhǔn)備工作:

工欲善其事必先利其器,因此我們有必要在進(jìn)行Coding前先配置一個(gè)適合我們自己的開(kāi)發(fā)環(huán)境,我搭建的開(kāi)發(fā)環(huán)境是:

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

這次的網(wǎng)絡(luò)爬蟲(chóng)需求背景我打算延續(xù)DotNet開(kāi)源大本營(yíng)在他的那篇文章中的需求,這里就不再詳解。我們只抓取某一省中所有主要城市從2015-11-22到2015-10-24的白天到夜間的所有天氣情況。這里以湖北省為例。
2.實(shí)戰(zhàn)網(wǎng)頁(yè)爬蟲(chóng):
2.1.獲取城市列表:
首先,我們需要獲取到湖北省所有城市的網(wǎng)頁(yè),然后進(jìn)行網(wǎng)頁(yè)解析。網(wǎng)絡(luò)地址為:http://www.tianqihoubao.com/weather/province.aspx?id=420000
我們查看該網(wǎng)頁(yè)的源碼可以發(fā)現(xiàn)所有的城市列表都是以 ,因此,我們可以封裝一個(gè)函數(shù)來(lái)通過(guò)使用正則表達(dá)式獲取我們想要的數(shù)據(jù),示例代碼如下所示:

復(fù)制代碼 代碼如下:
def? ShowCity():
??? html=requests.get(" http://www.tianqihoubao.com/weather/province.aspx?id=420000")
??? citys= re.findall(' ', html.text,re.S)
??? for city in citys:
??????? print city

抓取的結(jié)果如下所示:

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

          


2.2.獲取對(duì)應(yīng)城市的所有天氣信息:
然后我們需要根據(jù)抓取到的城市鏈接去抓取對(duì)應(yīng)城市的天氣情況,這里我們?cè)俜庋b一個(gè)函數(shù)用于顯示對(duì)應(yīng)城市的所有天氣狀況:

            
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('
            
              (.*?)
            
', html.text,re.S).group(1) res=re.findall('(.*?)', 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

這樣以來(lái),我們就可以獲取到了對(duì)應(yīng)城市的天氣情況了!!

完整代碼:

            
#coding:UTF-8
import re
import requests
import sys
reload(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('
            
              (.*?)
            
', html.text,re.S).group(1) res=re.findall('(.*?)', 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','*'*40 def ShowCity(): html=requests.get("http://www.tianqihoubao.com/weather/province.aspx?id=420000") citys= re.findall(' ', html.text,re.S) for city in citys: ShowWeather(city) def main(): ShowCity() if __name__=='__main__': main()

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

3.知識(shí)總結(jié):  

3.1.編碼問(wèn)題: 
#在ubuntu上,由于編碼問(wèn)題,我們需要在代碼的開(kāi)始位置添加一行注釋?zhuān)嬖VPyhton解釋器我們指定的編碼格式:

#此外,我們還需要設(shè)置默認(rèn)的編碼格式,否則Sublime Text會(huì)無(wú)法識(shí)別中文,報(bào)告一個(gè)錯(cuò)誤:“UnicodeEncodeError: 'ascii' codec can't encode characters in position”

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

          

3.2.正則表達(dá)式:

導(dǎo)入正則表達(dá)式庫(kù):import re
匹配任意字符:.
匹配前一個(gè)字符0次或無(wú)限次:*
匹配前一個(gè)字符0次或一次:?
貪心算法:.*
非貪心算法:.*?
匹配數(shù)字:(\d+)
常用函數(shù):

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

          

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


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 99精品国产成人a∨免费看 | 色九九亚洲偷偷动态图 | 一区二区三区在线免费 | 四虎精品永久在线 | 欧美高清亚洲欧美一区h | 色综合天天射 | 日本高清不卡二区 | 日产国产欧美视频一区精品 | 欧美精品久久久亚洲 | 日本在线观看不卡 | a级毛片在线视频免费观看 a级免费网站 | 黑人一级毛片 | 中文精品久久久久国产不卡 | 性久久 | 久青草资源福利视频 | 天天综合天天做天天综合 | 98在线视频噜噜噜国产 | 久久中文字幕2021精品 | 特级女人十八毛片a级 | 国产精品在线 | 国产精品成人一区二区1 | 日韩中文字幕视频在线 | 综合色图| 免费在线黄色网 | 日韩你懂的 | 97影院午夜在线观看视频 | 无遮挡又黄又爽又色1000部 | 一级片按摩 | 久久久久国产免费 | 亚洲黄色大片 | 福利影院在线看 | 99精品视频在线视频免费观看 | 久久久久中文 | 久久99热只有频精品6不卡 | 国产高清一级毛片在线不卡 | 国内精品免费久久久久妲己 | 日本-区二区三区免费精品 日本热久久 | 亚洲欧美午夜 | 国产 欧美 在线 | 一级黄片一级毛片 | 国产成人一区 |