python抓取某汽車網資料解析html存入excel樣本

來源:互聯網
上載者:User
1、某汽車網站地址

2、使用firefox查看後發現,此網站的資訊未使用json資料,而是簡單那的html頁面而已

3、使用pyquery庫中的PyQuery進行html的解析

頁面樣式:

代碼如下:


def get_dealer_info(self):
"""擷取經銷商資訊"""
css_select = 'html body div.box div.news_wrapper div.main div.news_list div.service_main div table tr '
#使用Firefox瀏覽器中的自動複製css路徑得到需要位置資料
page = urllib2.urlopen(self.entry_url).read()
#讀取頁面
page = page.replace('
','&')
page = page.replace('
','&')
#由於頁面中的電話資訊中使用了br換行,所以在抓取的時候會產生問題
#問題是:如果取得一對標籤中的資料,中包含
,會出現值得到br之前的資料,而後的資料將得不到,原因個人認為是解析html是會任務/>結尾標準
d = pq(page)
#使用PyQuery解析頁面,此處pq=PyQuery,因為from pyquery import PyQuery as pq
dealer_list = []
#建立列表用於提交到儲存方法
for dealer_div in d(css_select):
#此處定位tr,具體資料在此標籤中的td標籤內
p = dealer_div.findall('td')
#此處p就是一個tr標籤內,全部td資料的集合
dealer = {}
#此處的字典用於儲存一個店鋪的資訊用於提交到列表中
if len(p)==1:
#此處多哥if判斷是用於對資料進行處理,因為一些格式不符合最終資料的要求,需要剔除,這個快的代碼按需求而定
print '@'
elif len(p)==6 :
strp = p[0].text.strip()
dealer[Constant.CITY] = p[1].text.strip()
strc = p[2].text.strip()

dealer[Constant.PROVINCE] = p[0].text.strip()
dealer[Constant.CITY] = p[1].text.strip()
dealer[Constant.NAME] = p[2].text.strip()
dealer[Constant.ADDRESSTYPE] = p[3].text.strip()
dealer[Constant.ADDRESS] = p[4].text.strip()
dealer[Constant.TELPHONE] = p[5].text.strip()
dealer_list.append(dealer)
elif len(p)==5:
if p[0].text.strip() != u'省份':
dealer[Constant.PROVINCE] = strp
dealer[Constant.CITY] = p[0].text.strip()
dealer[Constant.NAME] = p[1].text.strip()
dealer[Constant.ADDRESSTYPE] = p[2].text.strip()
dealer[Constant.ADDRESS] = p[3].text.strip()
dealer[Constant.TELPHONE] = p[4].text.strip()
dealer_list.append(dealer)
elif len(p)==3:
print '@@'
print '@@@'
self.saver.add(dealer_list)
self.saver.commit()

4、最終代碼執行成功,得到了相應資料並存入excel中

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.