Python 抓取網頁的庫和工具
Twisted
抓網頁,它有優秀的非同步事件驅動的架構,常見的協定都已經有實做,包括HTTP、SMTP等等
getPage("http://www.google.com").addCallback(printPage)
一行就可以抓網頁
lxml
效率高,支持xpath
def getNextPageLink(self, tree):
"""Get next page link
@param tree: tree to get link
@return: Return url of next page, if there is no next page, return None
"""
paging = tree.xpath("http://span[@class='paging']")
if paging:
links = paging[0].xpath("./a[(text(), '%s')]" % self.localText['next'])
if links:
return str(links[0].get('href'))
return None
listPrice = tree.xpath("http://*[@class='priceBlockLabel']/following-sibling::*")
if listPrice:
detail['listPrice'] = self.stripMoney(listPrice[0].text)
使用的工具
FireFox的插件,XPath checker等xpath的工具,可以先用它來確定抓到的元素是正確的,然后FireBug在檢視網頁結構
posted on 2014-03-06 00:01 AlanTop 閱讀(593) 評論(1) 編輯 收藏 引用 所屬分類: 計算機應用