我试图首先在这个网站的主页上搜索每年一张表格的链接。然后,我要刮每个网站,同时保持每年的记录
到目前为止,我的蜘蛛构造为:
div = response.xpath('//*[@id="sidebar"]/div[1]/nav/ul/li[5]/div')
hrefs = div.xpath('*//a').extract()
splits = {}
for href in hrefs:
split = href.split('"')
link = split[1]
date = split[2]
clean_date = "".join(re.findall("[^><a/]",date))
clean_link = "http://www.ylioppilastutkinto.fi" + str(link)
splits[clean_date] = clean_link
然后,我将使用以下逻辑浏览此文件中的每个链接并对其进行爬网:
table = resp.xpath('//*[@id="content"]/table/tbody')
rows = table.xpath('//tr')
data_dict = {"Category":
[w3lib.html.remove_tags(num.get()) for num in rows[0].xpath('td')[1:]]
}
for row in rows[1:]:
data = row.xpath('td')
title = w3lib.html.remove_tags(data[0].get())
nums = [w3lib.html.remove_tags(num.get()) for num in data[1:]]
data_dict[title] = nums
我的问题是我找不到一个有效的方法。在url上调用scrapy.Request
返回一个只包含内容<html></html>
的响应。如果有一种方法,响应对象可以类似于Scrapy shell中的fetch
命令所给出的对象,那将是理想的,因为我已经基于使用该命令进行测试的选择逻辑
编辑:
这是目前为止所有的蜘蛛
其思想是运行第一个for循环以获取链接,然后运行第二个for循环以从所述链接提取表
import scrapy
import regex as re
from scrapy.http import HtmlResponse
import w3lib.html
class MainSpider(scrapy.Spider):
name = 'links'
allowed_domains = ['www.ylioppilastutkinto.fi/ylioppilastutkinto/pisterajat']
start_urls = ['https://www.ylioppilastutkinto.fi/ylioppilastutkinto/pisterajat/']
def parse(self, response):
div = response.xpath('//*[@id="sidebar"]/div[1]/nav/ul/li[5]/div')
hrefs = div.xpath('*//a').extract()
splits = {}
for href in hrefs:
split = href.split('"')
link = split[1]
date = split[2]
clean_date = "".join(re.findall("[^><a/]",date))
clean_link = "http://www.ylioppilastutkinto.fi" + str(link)
splits[clean_date] = clean_link
for date,url in splits.items():
resp = HtmlResponse(url)
table = resp.xpath('//*[@id="content"]/table/tbody')
rows = table.xpath('//tr')
data_dict = {"Category":[w3lib.html.remove_tags(num.get()) for num in rows[0].xpath('td')[1:]]}
for row in rows[1:]:
data = row.xpath('td')
title = w3lib.html.remove_tags(data[0].get())
nums = [w3lib.html.remove_tags(num.get()) for num in data[1:]]
data_dict[title] = nums
yield {
'Date': date,
'Scores': data_dict}
初始化
HtmlResponse(url)
不会完成任何事情,因为类本身不会发出请求要向scrapy的调度程序添加请求,您需要生成一个请求,例如:
yield scrapy.Request(url, callback=self.parse)
也就是说,您可以对spider进行许多改进
使用scrapy的内置^{} 而不是字符串拆分
使用css选择器而不是硬编码的XPath
使用
selector.root.text
而不是w3lib.remove_tags
(完全删除依赖项)以下是一个工作示例:
注意您的类别解析似乎不起作用。我不确定你想提取什么,所以我把这个留给你
相关问题 更多 >
编程相关推荐