Selenium Scrapy在无头模式下仍在打开Chrome浏览器



问题

  1. 我正在尝试以无头模式运行Selenium Scrapy scraper(下面的代码)
  2. Scraper在"令人兴奋"的模式下正常工作(打开Chrome浏览器)
  3. 当我从这里添加说明并再次运行时,刮刀运行起来就像什么都没有改变一样。也就是说,它像以前一样运行,并打开Chrome
  4. 在Windows计算机上工作。Chrome浏览器版本111

我应该更改什么以使其在无头模式下运行?我们非常感谢所有的建议。非常感谢。

代码

import scrapy
from scrapy_selenium import SeleniumRequest
import gspread
import scrapy 
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
ChromeOptions options = new ChromeOptions()
options.addArguments("--headless")
gc = gspread.service_account(filename = 'credentials2.json')
sh = gc.open_by_key('api_key').sheet1
class QuoteItem(scrapy.Item):
# define the fields for your item here like:
text = scrapy.Field()
author = scrapy.Field()
tags = scrapy.Field()
class QuotesSpider(scrapy.Spider):
name = 'techleapsesc'
def start_requests(self):
url = 'https://finder.techleap.nl/investors.accelerators'
yield SeleniumRequest(url=url, callback=self.parse, wait_time= 3)
def parse(self, response):
print("Line 24 - inside parse function")
quote_item = QuoteItem()

print("Line 27 - before for loop")
print(response.css('div'))
for quote in response.css('div.quote'):
quote_item['text'] = quote.css('span.text::text').get()
quote_item['author'] = quote.css('small.author::text').get()
quote_item['tags'] = quote.css('div.tags a.tag::text').getall()
self.sh.append_row(list(quote.values()))
print(quote)
yield quote_item

尝试了调用无头模式的不同方法

更改以下内容(Java代码):

ChromeOptions options = new ChromeOptions()
options.addArguments("--headless")

To(Python代码):

options= webdriver.ChromeOptions()
options.add_argument("--headless")

最新更新