![](/img/trans.png)
[英]How to I get Python Scrapy to extract all of the domains of all external links from a web page?
[英]How to get all links from a list of domains with Scrapy?
我认为我需要做的非常简单,但是我很难找到任何不仅仅关注于抓取单个域的优质资源。
我有大约9000个域的列表。 对于他们每个人,我必须检查在他们的域中是否存在指向我的网站的链接。 基本上,我需要该列表中链接回我的网站的网站列表。 因此,尽管URL的输入为9,000,但是我的代码的结果却要小得多。
非常感谢任何有关如何开始执行此操作的提示。 我已经完成了多个Scrapy教程,但这还不是我发现的信息。
编辑-这是我目前正在使用的蜘蛛:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from urllib.parse import urlparse
class JakeSpider(CrawlSpider):
name = 'jake'
allowed_domains = ['hivedigital.com','gofishdigital.com','quizzly.co']
start_urls = ['http://hivedigital.com/', 'http://gofishdigital.com/', 'https://quizzly.co/']
rules = (
Rule(LinkExtractor(allow=()), callback='parse_item', follow=True),
)
def parse_item(self, response):
#i = {}
page = response.url
domain = urlparse(page).netloc
print("............", domain)
links = response.xpath('//a/@href').extract()
#i['name'] = response.xpath('//div[@id="name"]').extract()
#i['description'] = response.xpath('//div[@id="description"]').extract()
#return i
le = LinkExtractor()
for link in le.extract_links(response):
if link.url == 'http://twitter.com':
yield {'link':link,'domain': domain}
您可以使用LinkExtractor
来获取所有链接,然后只需选择实际需要的链接即可。
from scrapy import Spider
from scrapy.linkextractors import LinkExtractor
class MySpider(Spider):
name = 'myspider'
start_urls = ['http://domain1.com', 'http://domain2.com', ...]
def parse(self, response):
le = LinkExtractor()
for link in le.extract_links(response):
if link.url == 'something I want':
# do something
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.