美文网首页
scrapy中使用Selector获取selenium响应页面

scrapy中使用Selector获取selenium响应页面

作者: 奈斯凸米特 | 来源:发表于2020-02-20 14:04 被阅读0次
    from selenium import webdriver
    from scrapy import Selector
    
    def set_driver(self):
            """
            设置webdriver
            """
            chrome = webdriver.ChromeOptions()
            chrome.headless = True  # 设置无头浏览器
            path = r'chromedriver/chromedriver.exe'
            driver = webdriver.Chrome(options=chrome, executable_path=path)
            return driver
    
    driver = self.set_driver()
    driver.get(url)
    # 获取webdriver响应页面
    selenium_response = Selector(text=driver.page_source, type='html')
    

    不仅仅是可以用于selenium,requests也可以,如下:

    r = requests.get(url=url, headers=headers, timeout=(30, 60))
    # 获取响应
    response = Selector(text=r.text, type='html')
    

    相关文章

      网友评论

          本文标题:scrapy中使用Selector获取selenium响应页面

          本文链接:https://www.haomeiwen.com/subject/vnrxqhtx.html