python - scrapy蜘蛛绕过拒绝我的规则

标签 python web-scraping scrapy

嗨,我正在尝试使用crawlspider,并且我创建了自己的拒绝规则

class MySpider(CrawlSpider): 
    name = "craigs" 
    allowed_domains = ["careers-cooperhealth.icims.com"] 
    start_urls = ["careers-cooperhealth.icims.com"] 
    d= [0-9] 
    path_deny_base = [ '.(login)', '.(intro)', '(candidate)', '(referral)', '(reminder)', '(/search)',] 
    rules = (Rule (SgmlLinkExtractor(deny = path_deny_base, 
                                     allow=('careers-cooperhealth.icims.com/jobs/…;*')), 
                                     callback="parse_items", 
                                     follow= True), ) 

我的蜘蛛仍然爬行类似 https://careers-cooperhealth.icims.com/jobs/22660/registered-nurse-prn/login 的页面登录名不应被抓取的地方有什么问题?

最佳答案

就这样改变它(没有点和括号):

deny = ['login', 'intro', 'candidate', 'referral', 'reminder', 'search']
allow = ['jobs']

rules = (Rule (SgmlLinkExtractor(deny = deny, 
                                 allow=allow, 
                                 restrict_xpaths=('*')), 
                                 callback="parse_items", 
                                 follow= True),)

这意味着提取的链接中没有loginintro等,仅提取其中包含jobs的链接。

这是抓取链接https://careers-cooperhealth.icims.com/jobs/intro?hashed=0并打印“YAHOO!”的完整蜘蛛代码:

from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule


class MySpider(CrawlSpider):
    name = "craigs" 
    allowed_domains = ["careers-cooperhealth.icims.com"] 
    start_urls = ["https://careers-cooperhealth.icims.com"]

    deny = ['login', 'intro', 'candidate', 'referral', 'reminder', 'search']
    allow = ['jobs']

    rules = (Rule (SgmlLinkExtractor(deny = deny,
                                     allow=allow,
                                     restrict_xpaths=('*')),
                                     callback="parse_items",
                                     follow= True),)

    def parse_items(self, response):
        print "YAHOO!"

希望有帮助。

关于python - scrapy蜘蛛绕过拒绝我的规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18482813/

相关文章:

python - 无法以正确的方式从网页收集标题

python - 如何将 scrapy-splash 与旋转代理一起使用?

python - 如何使用 scrapy 从具有数据库的网页中提取数据

python - multiprocessing.Value 无法正确存储 float

python - MATLAB ind2sub 和 Numpy unravel_index 不一致

python - Numpy:在 2D bool 数组中逐项快速计算 True 实例

javascript - 使用 SCRAPY 和 PYTHON 从 Javascript 中抓取数据

python - 减少程序计算大量输入的时间

python - Python 中 'wb' 文件模式下的 FileNotFoundError?

python - 在支持 POSTing 的情况下呈现不带 Selenium 的 JSON/Javascript 后从网页中抓取 HTML