xpath爬取新浪天氣,xpath爬新浪

來源:互聯網
上載者:User

xpath爬取新浪天氣,xpath爬新浪

參考資料:

http://cuiqingcai.com/1052.html

http://cuiqingcai.com/2621.html

http://www.cnblogs.com/jixin/p/5131040.html

完整代碼:

 1 # -*- coding:utf-8 -*- 2 import urllib2 3 from lxml import etree 4 user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0" 5 headers = {'User-Agent':user_agent} 6 url = 'http://weather.sina.com.cn/' 7 user_input = raw_input("請輸入你想查詢天氣的城市的拼音,如beijing\n") 8 # print user_input 9 url = url+user_input10 # print url11 req = urllib2.Request(url,headers=headers)12 reponse = urllib2.urlopen(req)13 text = reponse.read()14 # print html15 # print type(text)16 html = etree.HTML(text)17 # print html18 # result = etree.tostring(html)19 # print result20 # 有時候當天天氣資訊的icons和times資料只有19條,分開處理21 def change_list(lis):22     new_lis = []23     if len(lis) == 19:24         if lis == icons:25             new_lis.append(lis[0])26             for i in range(1,19,2):27                 new_lis.append(lis[i]+u'轉'+lis[i+1])28         elif lis == times:29             new_lis.append(lis[0].text)30             for i in range(1, 19, 2):31                 new_lis.append(lis[i].text + u'到' + lis[i + 1].text)32     elif len(lis) == 20:33         if lis == icons:34             for i in range(20,2):35                 new_lis.append(lis[i]+u'轉'+lis[i+1])36         elif lis == times:37             for i in range(20,2):38                 new_lis.append(lis[i].text + u'到' + lis[i + 1].text)39     return new_lis40 note1 = html.xpath('//*[@class="wt_tt0_note"]')41 note2 = html.xpath('//*[@class="wt_tt0_note"]/..')42 # print note[0].text43 dates = html.xpath('//*[@class="wt_fc_c0_i_date"]')44 days = html.xpath('//*[@class="wt_fc_c0_i_date"]/following-sibling::*[1]')45 icons = html.xpath('//*[@class="wt_fc_c0_i_icons clearfix"]/img/@alt')46 # print len(icons)47 icons = change_list(icons)48 times = html.xpath('//*[@class="wt_fc_c0_i_times"]/span')49 times = change_list(times)50 temps = html.xpath('//*[@class="wt_fc_c0_i_temp"]')51 tips = html.xpath('//*[@class="wt_fc_c0_i_tip"]')52 ls = html.xpath('//*[@class="l"]')53 rs = html.xpath('//*[@class="r"]')54 print note1[0].text,note2[0].text55 # print len(ls),len(rs)56 # PM2.5和空氣品質只有7條資料57 for i in range(7):58     print dates[i].text, days[i].text, times[i], icons[i], temps[i].text, tips[i].text, u'PM2.5:'+ls[i].text, u'空氣品質:'+rs[i].text59 for i in range(7,10):60     print dates[i].text, days[i].text, times[i], icons[i], temps[i].text, tips[i].text

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.