给我一个Scrapy和Selenium结合的python示例代码
以下是一个使用Scrapy和Selenium结合的Python示例代码:
import scrapy
from scrapy.selector import Selector
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from scrapy.http import HtmlResponse
class SeleniumSpider(scrapy.Spider):
name = "selenium_spider"
start_urls = [
'https://www.example.com',
]
def __init__(self):
chrome_options = Options()
chrome_options.add_argument("--headless") # 无界面模式
self.driver = webdriver.Chrome(chrome_options=chrome_options)
def parse(self, response):
self.driver.get(response.url)
sel = Selector(text=self.driver.page_source)
# 在这里使用Selenium模拟浏览器操作
search_input = self.driver.find_element_by_xpath('//input[@name="search"]')
search_input.send_keys("Scra