最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python制作簡單的網(wǎng)頁爬蟲

 更新時間:2015年11月22日 16:41:10   投稿:hebedich  
自己寫的一個爬蟲,模仿了python核心編程書里的程序,有詳細(xì)的注釋。 是我一個理解學(xué)習(xí)的過程吧。 有需要的小伙伴可以參考下

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

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

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

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

復(fù)制代碼 代碼如下:

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

 1 top/anlu.html" title="安陸歷史天氣查詢
 2 top/badong.html" title="巴東歷史天氣查詢
 3 top/baokang.html" title="??禋v史天氣查詢
 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="當(dāng)陽歷史天氣查詢
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="監(jiān)利歷史天氣查詢
29 top/jianshi.html" title="建始?xì)v史天氣查詢
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="神農(nóng)架歷史天氣查詢
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="宜都?xì)v史天氣查詢
69 top/yingcheng.html" title="應(yīng)城歷史天氣查詢
70 top/hbyingshan.html" title="英山歷史天氣查詢
71 top/yuanan.html" title="遠(yuǎn)安歷史天氣查詢
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.獲取對應(yīng)城市的所有天氣信息:
然后我們需要根據(jù)抓取到的城市鏈接去抓取對應(yīng)城市的天氣情況,這里我們再封裝一個函數(shù)用于顯示對應(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('<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

這樣以來,我們就可以獲取到了對應(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('<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','*'*40

def 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個月的所有天氣情況,是不是很厲害呀?。。???不過不要高興的太早,凡事有利有弊,看看它的運行結(jié)果吧:[Finished in 371.8s]

3.知識總結(jié):  

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

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

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

3.2.正則表達式:

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

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

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

相關(guān)文章

  • python基于tkinter制作m3u8視頻下載工具

    python基于tkinter制作m3u8視頻下載工具

    這篇文章主要介紹了python如何基于tkinter制作m3u8視頻下載工具,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • Python Socket TCP雙端聊天功能實現(xiàn)過程詳解

    Python Socket TCP雙端聊天功能實現(xiàn)過程詳解

    這篇文章主要介紹了Python Socket TCP雙端聊天功能實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • 淺談python3.x pool.map()方法的實質(zhì)

    淺談python3.x pool.map()方法的實質(zhì)

    這篇文章主要介紹了python3.x pool.map方法的實質(zhì),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • python常用的魔法方法(雙下劃線)

    python常用的魔法方法(雙下劃線)

    本文介紹一下python中常用的魔法方法以及面向?qū)ο笾蟹浅V匾膯卫J?。具有一定的參考價值,感興趣的可以了解一下
    2021-09-09
  • python學(xué)習(xí)實操案例(三)

    python學(xué)習(xí)實操案例(三)

    這篇文章主要介紹了python學(xué)習(xí)實操案例,循環(huán)輸出26個字母對應(yīng)的ASCII碼值、模擬用戶登錄、猜數(shù)游戲練習(xí)等案例,需要的小伙伴可以參考一下,希望對你有一定的幫助
    2022-02-02
  • tensorflow 加載部分變量的實例講解

    tensorflow 加載部分變量的實例講解

    今天小編就為大家分享一篇tensorflow 加載部分變量的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python中pika模塊問題的深入探究

    python中pika模塊問題的深入探究

    這篇文章主要給大家介紹了關(guān)于python中pika模塊問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • 淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點

    淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點

    這篇文章主要介紹了淺談tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意點,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • python基于openpyxl生成excel文件

    python基于openpyxl生成excel文件

    這篇文章主要介紹了python基于openpyxl生成excel文件的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • python 將md5轉(zhuǎn)為16字節(jié)的方法

    python 將md5轉(zhuǎn)為16字節(jié)的方法

    今天小編就為大家分享一篇python 將md5轉(zhuǎn)為16字節(jié)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05

最新評論

涡阳县| 本溪市| 平果县| 南澳县| 蛟河市| 鸡泽县| 裕民县| 遂昌县| 民勤县| 太康县| 长春市| 新安县| 东丰县| 奉化市| 安福县| 东丽区| 乡宁县| 隆德县| 龙江县| 曲阳县| 陵水| 广河县| 阜新| 团风县| 金山区| 大荔县| 武威市| 蒙阴县| 玛多县| 枝江市| 铜山县| 中方县| 通州区| 铜鼓县| 无棣县| 易门县| 达孜县| 酉阳| 报价| 客服| 旬邑县|