[英]How to I get Python Scrapy to extract all of the domains of all external links from a web page?
我希望循環檢查每個鏈接 - 如果它轉到 output 它的外部域 - 目前它輸出所有鏈接(內部和外部)。 我搞砸了什么? (為了測試,我已將代碼調整為僅從單個頁面運行,而不是爬取站點的 rest。)
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
您的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
如果您真的只想要域,那么您可以使用urlparse
中的urllib.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.