Scrapy-努力检索文本

时间:2018-10-25 18:50:43

标签: python web-scraping scrapy

我正在尝试使用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中,但也许不是吗? ...

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

以下是我更新的日志文件中的重点内容:

1 个答案:

答案 0 :(得分:0)

我怀疑您收到此错误,因为<a href="some URL">内的一个(或多个)网址实际上是其他 而不是链接的内容一个网页。因此,在这种情况下,它看起来像是将电子邮件发送到特定电子邮件地址的链接

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

OR

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

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