我已禁用Default Scrapy cookie选项,因此我必须手动设置它.
COOKIES_ENABLED = False
COOKIES_DEBUG = True
现在,我需要使用作为同一站点的响应接收的值来设置cookie.我能够得到如下的cookie,
cookie = response.headers.getlist('Set-Cookie')[0].split(";")[0].split("=")
现在我正在尝试将其设置为表单请求
FormRequest.from_response(response,
                formdata={"username": "asldkfs", "pass": "slskd"},
                cookies={cookie[0]:cookie[1]},
                meta = {'dont_redirect': True,'handle_httpstatus_list': [302]},
                callback=self.redirection)
def redirection(self,response): 
    self.log("redirection")
    self.log(response.headers)               
    self.log("Cookie2")
    cook1 = response.headers.getlist('Set-Cookie')[0].split(";")[0].split("=")
    self.log(cook1)        
    self.log("end cookie2")
    return Request("http://something.net/some/sa/"+response.headers.getlist('Location')[0],cookies={cook1[0]:cook1[1]},
        callback=self.check_login_response)
.
.
.
所以我无法设置cookie.我还需要设置任何其他值或可能是什么问题?
我已经有一段时间进行网络抓取,但对 python 来说相对较新,最近将我所有的抓取活动从 ruby 切换到 python,主要是因为scrapy和scrapinghub似乎为大规模生产化抓取提供了更好的支持。
我在抓取电子商务网站时遇到的一个问题是,许多似乎使用“有状态”会话,即除非您发送从先前响应返回的相同 cookie,否则下一个请求可能会被阻止。特别是,许多使用IBM Websphere 的站点都表现出这种行为。
鉴于使用并发异步请求,这成为scrapy 的一个问题。
这些站点中的大多数都需要加载 JS 才能设置初始 cookie,因此我的方法是使用 Selenium(无头 chromedriver)加载初始页面,然后将 cookie 传递给普通的 scrapy 请求。
def __initialise_cookies(self):
        # Where self is the spider and driver is the Selenium driver instance
        self.session_cookies = self.driver.get_cookies()
当 CONCURRENT_REQUESTS 在 scrapy 配置文件中设置为 1 时,这种方法完全正常。然而,这消除了所有并发性,显然会大大减慢刮擦速度。
我知道scrapy 已经发布了下载器中间件功能,允许在请求中命名cookiejar,然后传递给后续请求。我也读过这篇文章。然而,这似乎并没有解决我的问题 - 我只能假设是因为并发导致 cookiejar 被同时重复使用多次,即使您创建几个不同的 cookiejar 作为起点。
有没有人有关于如何解决这个问题的想法?
理想情况下,我想创建与 CONCURRENT_REQUESTS 设置(例如 16)相同数量的会话 cookiejar,但是我如何处理确保每个 cookiejar 一次最多只使用一次,然后将响应 cookie 传递给下一个请求。
我知道 Twisted 不使用线程,但是为 N 个 cookiejar 中的每一个创建一个信号量并使请求等待直到它未使用后再发送下一个请求是否有意义?
我只是想知道是否有任何合理的方法可以将身份验证 cookie 从 webdriver.Firefox() 实例传递给蜘蛛本身?执行一些 webdriver 的东西然后去抓取“一切照旧”会很有帮助。有以下作用:
def __init__(self):
    BaseSpider.__init__(self)
    self.selenium = webdriver.Firefox()
def __del__(self):
    self.selenium.quit()
    print self.verificationErrors
def parse(self, response):
    # Initialize the webdriver, get login page
    sel = self.selenium
    sel.get(response.url)
    sleep(3)
    ##### Transfer (sel) cookies to (self) and crawl normally??? #####
    ...
    ...
我正在使用 scrapy 来抓取阿迪达斯网站:http://www.adidas.com/us/men-shoes。但它总是显示错误:
用户超时导致连接失败:获取http://www.adidas.com/us/men-shoes花费的时间超过 180.0 秒..
它重试了 5 次,然后完全失败。
我可以访问 chrome 上的 url,但它在scrapy 上不起作用。
我试过使用自定义用户代理并模拟标头请求,但它仍然不起作用。
下面是我的代码:
import scrapy
class AdidasSpider(scrapy.Spider):
    name = "adidas"
    def start_requests(self):
        urls = ['http://www.adidas.com/us/men-shoes']
        headers = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "en-US,en;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Host": "www.adidas.com",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
        }
        for url in urls:
            yield scrapy.Request(url, self.parse, headers=headers)
    def parse(self, response):
        yield(response.body)
爬虫日志:
{'downloader/exception_count': 1,
 'downloader/exception_type_count/twisted.web._newclient.ResponseNeverReceived': …这是我的代码
class Test(Spider):
    self.settings.overrides['JOBDIR']= "seen"
我有:
  File "C:\Python27\lib\site-packages\scrapy\spider.py", line 46, in settings
    return self.crawler.settings
  File "C:\Python27\lib\site-packages\scrapy\spider.py", line 41, in crawler
    assert hasattr(self, '_crawler'), "Spider not bounded to any crawler"
AssertionError: Spider not bounded to any crawler
我正在扩展Spider,我没有使用,Crawler因为我没有链接或规则可以遵循
我猜我的问题是因为我没有很好地导入设置,我需要你的帮助
scrapy ×5
python ×4
cookies ×2
python-2.7 ×1
redirect ×1
scrapinghub ×1
selenium ×1
twisted ×1
web-scraping ×1
webdriver ×1