【问题标题】:Scrapy - Struggling with retrieving textScrapy - 努力检索文本
【发布时间】:2018-10-26 07:38:22
【问题描述】:

我正在尝试使用 Scrapy 构建一个抓取工具,但我正在努力返回所需的文本。任何帮助将不胜感激。

这是我的代码:

import scrapy
from scrapy.spiders import Request
from scrapy.linkextractors import LinkExtractor
from scrapy.http import HtmlResponse  
import re
from urllib import *


BASE_URL = 'http://murderpedia.org/'
PROTOCOL = 'https:'

这是我的物品类别

class CornFlakeItem(scrapy.Item):

    name = scrapy.Field()
    bio = scrapy.Field()
    images = scrapy.Field()
    link = scrapy.Field()
    image_urls = scrapy.Field()
    bio_image = scrapy.Field()
    image_paths = scrapy.Field()  

    classification = scrapy.Field()
    characteristics = scrapy.Field()
    number_of_victims = scrapy.Field()
    date_of_murders = scrapy.Field()
    date_of_birth = scrapy.Field()
    victims_profile = scrapy.Field()
    method_of_murder = scrapy.Field()
    location = scrapy.Field()
    status = scrapy.Field()

这是我的生物课:

class CornFlakeBio(scrapy.Spider):
    name = 'corn-flake-killers'
    start_urls = ['http://murderpedia.org/male.A/index.A.htm']

这是我的解析函数:

    def parse(self, response):

        table= 
        response.xpath('//td[contains(font//font/text(),
        "Victims")]/../..')
        urls = table.xpath('//a/@href').extract()
        for url in urls:
            if (url.startswith('mailto:')): 
                yield None
            else:
                yield Request(response.urljoin(url), self.parse_person)

这是我的解析人功能:

    def parse_person(self, response):

        table = response.xpath('//*[@id="table4"]')
        for row in table.xpath('//tbody'):


            text = {
             'Classification' : 
             row.xpath('//tr[3]/td/style/text()').extract_first(),
             'Characteristics': 
             row.xpath('//tr[4]/td/style/text()').extract_first(),
             'Number of Victims' : 
             row.xpath('//tr[5]/td/style/text()').extract_first(),
            'Date of Murders': 
             row.xpath('//tr[6]/td/style/text()').extract_first(),
            'Date of Birth': 
             row.xpath('//tr[7]/td/style/text()').extract_first(), 
            'Victims Profile': 
             row.xpath('//tr[8]/td/style/text()').extract_first(), 
            'Method of Murder': 
             row.xpath('//tr[9]/td/style/text()').extract_first(),  
            'Location' : 
            row.xpath('//tr[10]/td/style/text()').extract_first(),
            'Status' : 
            row.xpath('//tr[11]/td/style/text()').extract_first()}

            text = ''.join(text) 

            print(text)[:10] 

我感觉我的问题在于每一行的 xpath,但也许不是? ...

这里的任何帮助将不胜感激。

以下是我更新的日志文件中的亮点:

【问题讨论】:

  • 你得到什么错误/输出?
  • @AlphaTested 感谢您的回复。我在上面添加了日志文件

标签: python web-scraping scrapy


【解决方案1】:

我怀疑您收到此错误是因为您在 <a href="some URL"> 中的一个(或多个)网址实际上是 other than 指向网络的链接页。所以在这种情况下,它看起来可能是一个将电子邮件发送到特定电子邮件地址的链接

您可以使用来自 scrapy 的链接提取器: https://doc.scrapy.org/en/latest/topics/link-extractors.html

您可以在 Python 中进行一些字符串过滤,以注意诸如锚标签(以 # 开头)或电子邮件地址(通常以 mailto 开头)等内容

我不久前给出的这个答案可能会为可选阅读提供一些额外的上下文: https://stackoverflow.com/a/52900592/9693088

【讨论】:

  • 我在原始代码中进行了您推荐的更改。我不再收到“mailto”错误,实际上我不再收到任何错误,我只是看不到任何文本正在生成。你或其他人知道我在这里做错了什么吗?
  • 我不确定,一个好的步骤是使用 scrapy shell 尝试进一步调试:doc.scrapy.org/en/latest/topics/shell.html
猜你喜欢
  • 2022-11-01
  • 2015-01-09
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 2020-07-12
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多