我正在使用Scrapy,脚本遇到问题。它在外壳上工作正常:
[scrapy shell "www.redacted.com"
我使用response.xpath("//li[@a data-urltype()"]).extract
我能够从页面中抓取200个左右的链接。
这是我要抓取的网页中的代码:
<a data-urltype="/view" data-mce-href="http://www.redacted.aspx?ID=xxxxxxxxxx" data-linktype="external" href="http://www.redacted.com/Home/wfContent.aspx?xxxxxxxxxxxxx" data-val="http://www.redacted.gov/Home/wfContent.aspx?xxxxxxxxxxxx" target="_blank">link text</a>
我的问题是脚本:(在下面发布)我知道"a data-val"
错误。
import scrapy
from ..items import LinkscrapeItem
class Linkscrape(scrapy.Spider):
name = 'lnkscrapespider'
start_urls = [
'https://www.redacted.com'
]
def parse(self, response):
items = LinkscrapeItem()
links = response.xpath("a data-val").xpath.extract()
for links in links:
items['links'] = links
yield{
'links': links
}
如果要从data-val
刮取a
。在xpath下使用。
links = response.xpath("//li/a/@data-val").xpath.extract()