【问题标题】:Scraperwiki Python Loop IssueScraperwiki Python 循环问题
【发布时间】:2014-11-24 09:39:36
【问题描述】:

我正在使用 Python 通过 ScraperWiki 创建一个刮板,但我得到的结果有问题。我的代码基于 ScraperWiki 文档上的 basic example,一切看起来都非常相似,所以我不确定我的问题出在哪里。对于我的结果,我得到了页面上的第一个文档标题/URL,但循环似乎存在问题,因为它不会返回该文档之后的剩余文档。任何建议表示赞赏!

import scraperwiki
import requests
import lxml.html

html = requests.get("http://www.store.com/us/a/productDetail/a/910271.htm").content
dom = lxml.html.fromstring(html)

for entry in dom.cssselect('.downloads'):
    document = {
        'title': entry.cssselect('a')[0].text_content(),
        'url': entry.cssselect('a')[0].get('href')
    }
    print document

【问题讨论】:

    标签: python web-scraping css-selectors lxml scraperwiki


    【解决方案1】:

    您需要使用类downloads 遍历div 内的a 标签:

    for entry in dom.cssselect('.downloads a'):
        document = {
            'title': entry.text_content(),
            'url': entry.get('href')
        }
        print document
    

    打印:

    {'url': '/webassets/kpna/catalog/pdf/en/1012741_4.pdf', 'title': 'Rough In/Spec Sheet'}
    {'url': '/webassets/kpna/catalog/pdf/en/1012741_2.pdf', 'title': 'Installation and Care Guide with Service Parts'}
    {'url': '/webassets/kpna/catalog/pdf/en/1204921_2.pdf', 'title': 'Installation and Care Guide without Service Parts'}
    {'url': '/webassets/kpna/catalog/pdf/en/1011610_2.pdf', 'title': 'Installation Guide without Service Parts'}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多