Scraperwiki Python循环Issu

2024-06-16 09:36:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python通过ScraperWiki创建一个scraper,但是我对得到的结果有一个问题。我的代码是基于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

Tags: 代码文档importgetbasichtmlcontentscraper
1条回答
网友
1楼 · 发布于 2024-06-16 09:36:27

您需要使用类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'}

相关问题 更多 >