我正在尝试让我的抓取蜘蛛拒绝.com域名。传递给deny_domains的正确字符串是什么?我试过"*.com“,但它不起作用。
问题更新:我如何才能反过来做呢?例如,如果我只想抓取.com域
import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from myproject.items import MyprojectItem
class pformSpider(CrawlSpider):
name = "pform6"
start_urls = [
"http://example.se",
]
extractor = SgmlLinkExtractor(deny_domains=("*.com"))
rules = (
Rule(extractor,callback='parse_links',follow=True),
)
def parse_links(self, response):
item = MyprojectItem()
item['url'] = response.url
yield item发布于 2016-05-21 07:41:43
您可以使用scrapy.linkextractors
来自http://doc.scrapy.org/en/latest/topics/link-extractors.html
deny_domains (字符串或列表)-包含不会在提取链接时考虑的域的单个值或字符串列表
但
deny (正则表达式(或正则表达式列表))-(绝对) urls必须匹配才能排除的单个正则表达式(或正则表达式列表)。未提取)。
所以你可以使用正则表达式和"deny“,我猜就像这样
".*\.com\/.*"但它可能与URL中的其他位置匹配。
发布于 2016-05-21 07:42:39
from scrapy.linkextractors import LinkExtractor
...
rules=(
Rule(LinkExtractor(deny=('.+\.com', ))),
)发布于 2016-05-24 18:05:14
基于documentation,我会说你需要这样做:
extractor = SgmlLinkExtractor(allow="*.com")注意:我没有对此进行测试。
参数: allow (字符串或列表)-一个正则表达式(或正则表达式列表),(绝对) urls必须匹配才能提取。如果未指定(或为空),它将匹配所有链接。
https://stackoverflow.com/questions/37357236
复制相似问题