我试图从this网站收集2000年的天气数据。
我的蜘蛛代码是:
import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors import LinkExtractor
class weather(CrawlSpider):
name = 'data'
start_urls = [
'https://www1.ncdc.noaa.gov/pub/data/uscrn/products/daily01/'
]
custom_settings = {
'DEPTH_LIMIT': '2',
}
rules = (
Rule(LinkExtractor(restrict_xpaths=
('//table/tr[2]/td[1]/a',)),callback='parse_item',follow=True),
)
def parse_item(self, response):
for b in response.xpath('//table')
yield scrapy.request('/tr[4]/td[2]/a/@href').extract()
yield scrapy.request('/tr[5]/td[2]/a/@href').extract()
带有'产生的路径'是两个文本文件的链接,我想从这些文本文件中抓取数据并将其分别存储在两个不同的文件中,但我不知道如何继续。
答案 0 :(得分:0)
我通常不会使用CrawlSpider
所以我不熟悉它,但似乎你应该能够创建另一个xpath(最好是比"/tr[4]/td[2]/a/@href"
更具体的东西)并提供回调函数。
然而,在一个典型的"使用Spider
代替CrawlSpider
的scrapy项目,您只需生成一个带有另一个回调函数的Request
来处理提取和存储到数据库。例如:
def parse_item(self, response):
for b in response.xpath('//table')
url = b.xpath('/tr[4]/td[2]/a/@href').extract_first()
yield scrapy.Request(url=url, callback=extract_and_store)
url = b.xpath('/tr[5]/td[2]/a/@href').extract_first()
yield scrapy.Request(url=url, callback=extract_and_store)
def extract_and_store(self, response):
"""
Scrape data and store it separately
"""