我正在使用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
}
答案 0 :(得分:1)
您无需重复使用[10][3]
:
.xpath()
下面也没有意义(也许您需要links = response.xpath("//li/a/@data-val").extract()
# or
links = response.xpath("//li/a/@data-val").getall()
?):
for link in links
答案 1 :(得分:0)
如果您要从data-val
抓取a
。在xpath下使用。
links = response.xpath("//li/a/@data-val").xpath.extract()