zoukankan      html  css  js  c++  java
  • 如果手工启动chromedriver

        使用selenium模拟登陆网站时,有些网站会识别chrome driver里的json信息,从而判断是不是爬虫程序,做到反爬效果。(比如知乎)

    下面说明下怎么手动启动chromedriver

    1)、启动chrome

    给开始菜单里的chrome或者桌面快捷方式,右键打开文件夹所在路径

    2)打开dos,切到chrome路径,执行:chrome.exe --remote-debugging-port=9090(端口自定义),表示以debug模式启动,监听端口是9090

    3)获取json,在启动前,需确保所有chrome实例已经关闭,否则会返回拒绝连接。

    class ZhihuSpider(scrapy.Spider):
        name = 'zhihu_2'
        allowed_domains = ['zhihu.com']
        start_urls = ['http://zhihu.com/']
    
        headers = {
            "HOST": "www.zhihu.com",
            "Referer": "https://www.zhizhu.com",
            'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
        }
        custom_settings = {
            "COOKIES_ENABLED": True,
            "HTTPERROR_ALLOWED_CODES" : [400]
        }
    
        def parse(self, response):
            from selenium import webdriver
            from scrapy.selector import Selector
            from selenium.webdriver.chrome.options import Options
            chrome_option = Options()
            chrome_option.add_argument("--disable-extensions")
            chrome_option.add_experimental_option("debuggerAddress","127.0.0.1:9090")
            project_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    
            chromedriver_dir = os.path.join(project_dir, "tools\chromedriver.exe")
            browser = webdriver.Chrome(
                executable_path=chromedriver_dir,chrome_options=chrome_option)
    
            browser.get("https://www.zhihu.com/signup?next=%2F")
    
            # 模拟登录知乎,选择登录选项
            info = response.xpath('//*[@id="root"]/div/main/div/div/div/div[2]/div[2]/span/text()')
            browser.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div/div[2]/div[2]/span').click()
            # 输入账号//*div[@class='SignFlow-accountInput Input-wrapper']/input
            browser.find_element_by_xpath(
                '//*[@id="root"]/div/main/div/div/div/div[2]/div[1]/form/div[1]/div[2]/div[1]/input').send_keys(
                "656521736@qq.com")
            # 输入密码
            browser.find_element_by_xpath(
                '//*[@id="root"]/div/main/div/div/div/div[2]/div[1]/form/div[2]/div/div[1]/input').send_keys("*****")
            # 模拟登录知乎,点击登录按钮
             #//*[@id="root"]/div/main/div/div/div/div[2]/div[1]/form/button
            # browser.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div/div[2]/div[1]/form/button').click()
    
        # def start_requests(self):
        #
            return [
                Request('https://www.zhihu.com/signup?next=%2F', headers=self.headers, encoding="utf-8", dont_filter=True, callback=self.parse)]

    调用

    webdriver.Chrome方法是,加上一个参数chrome_options即可
  • 相关阅读:
    Django 用ModelForm批量保存form表单(非常实用的方法) mfor_verity项目
    jquery ajax异步提交表单数据的方法
    python字符串转换成变量的几种方法
    django 线上线下使用不同的数据库 上线:mysql 线下sqlite3 以及debug模式的开和关
    django admin 或xdmin list_display search_fields list_filter 如果显示搜索外键或多对多字段
    nonce和timestamp在Http安全协议中的作用
    Web API接口 安全验证
    .Net环境下的缓存技术介绍
    .Net缓存管理框架CacheManager
    在asp.net web api中利用过滤器设置输出缓存
  • 原文地址:https://www.cnblogs.com/laonicc/p/10880460.html
Copyright © 2011-2022 走看看