【发布时间】:2012-07-09 17:59:18
【问题描述】:
我怀疑这是一个微不足道的查询,但希望有人可以帮助我解决我在尝试构建的刮板中使用 lxml 得到的查询。
https://scraperwiki.com/scrapers/thisisscraper/
我正在逐行阅读教程 3,到目前为止,我一直在尝试提取下一页链接。我可以使用 cssselect 来识别链接,但我无法弄清楚如何仅隔离 href 属性而不是整个锚标记。
谁能帮忙?
def scrape_and_look_for_next_link(url):
html = scraperwiki.scrape(url)
print html
root = lxml.html.fromstring(html) #turn the HTML into lxml object
scrape_page(root)
next_link = root.cssselect('ol.pagination li a')[-1]
attribute = lxml.html.tostring(next_link)
attribute = lxml.html.fromstring(attribute)
#works up until this point
attribute = attribute.xpath('/@href')
attribute = lxml.etree.tostring(attribute)
print attribute
【问题讨论】:
标签: python-2.7 lxml scraper scraperwiki