首页 > 解决方案 > 如何让 Python Scrapy 从网页中提取所有外部链接的所有域?

问题描述

我希望循环检查每个链接 - 如果它去外部域输出它 - 目前它输出所有链接(内部和外部)。我搞砸了什么?(为了测试,我调整了代码,只在一个页面上工作,而不是爬取网站的其余部分。)

import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
import re

class MySpider(CrawlSpider):
    name = 'crawlspider'
    allowed_domains = ['en.wikipedia.org']
    start_urls = ['https://en.wikipedia.org/wiki/BBC_News']

    rules = (
        Rule(LinkExtractor(), callback='parse_item', follow=False),
    )

    def parse_item(self, response):
        item = dict()
        item['url'] = response.url
        item['title']=response.xpath('//title').extract_first()
        for link in LinkExtractor(allow=(),deny=self.allowed_domains).extract_links(response):
            item['links']=response.xpath('//a/@href').extract()
        return item

标签: pythonscrapy

解决方案


您方法中的逻辑parse_item看起来不太正确

def parse_item(self, response):
    item = dict()
    item['url'] = response.url
    item['title']=response.xpath('//title').extract_first()
    for link in LinkExtractor(allow=(),deny=self.allowed_domains).extract_links(response):
        item['links']=response.xpath('//a/@href').extract()
    return item

您正在从提取器中循环遍历每个link内容,但始终设置item["links"]为完全相同的内容(来自响应页面的所有链接)。我希望您尝试item["links"]将所有链接设置为LinkExtractor? 如果是这样,您应该将方法更改为

def parse_item(self, response):
    item = dict()
    item['url'] = response.url
    item['title'] = response.xpath('//title').extract_first()
    links = [link.url for link in LinkExtractor(deny=self.allowed_domains).extract_links(response)]        
    item['links'] = links
    return item

如果您真的只想要域,那么您可以使用urlparsefromurllib.parse来获取netloc. 您可能还想用set. 所以你的解析方法会变成(最好在文件顶部导入)

def parse_item(self, response):
    from urllib.parse import urlparse
    item = dict()
    item["url"] = response.url
    item["title"] = response.xpath("//title").extract_first()
    item["links"] = {
        urlparse(link.url).netloc
        for link in LinkExtractor(deny=self.allowed_domains).extract_links(response)
    }   
    return item

推荐阅读