Skip to main content
 首页 » 编程设计

python之Scrapy之从多个页面解析数据

2024年10月25日12davidwang456

我正在使用 Scrapy 抓取网站,从各个页面提取数据,然后将抓取的数据存储到列表中。我正在尝试从第一页抓取名称、url 和位置,然后从抓取的 url 遍历到该页面并从该页面抓取 DOCTYPE。请参阅下面我制作的代码,我一直在关注这个 documentation 密切关注,但我得到了奇怪的结果。

如果我不尝试在我的 ExampleSpider 中使用第二种方法,我会得到一个包含 3000 多个结果的列表,这正是我想要的……减去第二条基本数据。当我尝试包含此方法时,我得到的只是起始 URL(即 http://www.example.com、http://www.example1.com 等)。

关于我在这里做错了什么有什么建议吗?

import scrapy 
from scrapy.contrib.loader import ItemLoader 
from example.items import Item 
from scrapy.http import Request 
import re 
 
class ExampleSpider(scrapy.Spider): 
    name = "example" 
    allowed_domains = ["example.com"] 
    start_urls = ["list of about 15 different websites (ie. 'http://example.com', 'http://example1.com')"] 
 
    # #Working method to scrape all of the data I need (except DOCTYPE from second page) 
    # def parse(self, response): 
    #   for sel in response.xpath('//table[@class="rightLinks"]/tr'): 
    #     item = Item() 
    #     item['company_name'] = sel.xpath('td[1]/a/text()').extract() 
    #     item['website'] = sel.xpath('td[1]/a/@href').extract() 
    #     item['location'] = sel.xpath('td[2]/text()').extract() 
    #     yield item 
 
 
    def parse(self, response): 
      for sel in response.xpath('//table[@class="rightLinks"]/tr'): 
        item = PharmaItem() 
        item['company_name'] = sel.xpath('td[1]/a/text()').extract() 
        item['website'] = sel.xpath('td[1]/a/@href').extract() 
        item['location'] = sel.xpath('td[2]/text()').extract() 
        #Setting up a new request to pass to the get_DT method, also passing along the 'item' class meta data 
 
        #converting website from list item to string 
        website = ''.join(item['website']) 
        request = scrapy.Request(website, callback=self.get_DT) 
        request.meta['item'] = item 
 
        return request 
 
    #Get DOCTYPE from each page 
    def get_DT(self, response): 
        item = response.meta['item'] 
        item['website'] = response.url 
        dtype  = re.search("<!\s*doctype\s*(.*?)>", response.body, re.IGNORECASE) 
        item['DOCTYPE'] = dtype 
 
        yield item 

更新 这是我使用过的两个最终有效的函数。我取消了 whitelotus 的建议并进行了尝试,但这没有用,因为它一直返回父 DOCTYPE 而不是遍历的页面 DOCTYPE。

  def parse(self, response): 
    for sel in response.xpath('//table[@class="rightLinks"]/tr'): 
      item = PharmaItem() 
      item['company_name'] = sel.xpath('td[1]/a/text()').extract() 
      website = sel.xpath('td[1]/a/@href').extract()[0] 
      item['location'] = sel.xpath('td[2]/text()').extract() 
 
      # Setting up a new request to pass to the get_DT method, also passing along the 'item' class meta data 
      request = scrapy.Request(website, callback=self.get_DT) 
      request.meta['item'] = item 
      yield request 
 
  #Takes in the websites that were crawled from previous method and finds DOCTYPES 
  def get_DT(self, response): 
    item = response.meta['item'] 
    item['DOCTYPE'] = response.selector._root.getroottree().docinfo.doctype 
    item['website'] = response.url 
 
    yield item 

请您参考如下方法:

您正在运行一个循环,但在其中调用了 return。它将阻止循环遍历所有链接。在 parse() 函数中使用 yield。

除此之外,我不明白这部分:

#converting website from list item to string 
website = ''.join(item['website']) 

这似乎是错误的。如果那里有多个 Web URL,这将导致非常糟糕且无效的 URL。如果只有其中一个,那么您应该通过获取第一个也是唯一的列表元素来收集它(注意末尾的 [0]):

item['website'] = sel.xpath('td[1]/a/@href').extract()[0] 

此外,我不确定您为什么要在 parse() 函数中设置 item['website'],因为您将在get_DT 函数。你应该只使用一个临时变量,像这样:

for sel in response.xpath('//table[@class="rightLinks"]/tr'): 
    item = PharmaItem() 
    item['company_name'] = sel.xpath('td[1]/a/text()').extract() 
    item['location'] = sel.xpath('td[2]/text()').extract() 
    website = sel.xpath('td[1]/a/@href').extract() 
    request = scrapy.Request(website, callback=self.get_DT) 
    request.meta['item'] = item 
    yield request