zoukankan      html  css  js  c++  java
  • 记一次python + selenium小项目出现的问题与解决办法

    记一次python + selenium小项目出现的问题与解决办法

    如何接入代理

    def crawl_xdaili(self):#代理  可不用  需要时 解除注释
        """
        获取讯代理
        :return: 代理
        """
        url = '寻代理的api接口 自己去讯代理官网'
        r = requests.get(url)
        if r:
            result = json.loads(r.text)
            proxies = result.get('RESULT')
            for proxy in proxies:
                self.proxies = {
                    'http':'http://' + proxy.get('ip') + ":"+ proxy.get('port')
                }
                print(proxies)
                
                
          options.add_argument("--proxy-server=%s"%ip)#设置IP代理
    

    获取信息方式

    尽量使用第一次找到一个大的信息所在的标签通过xpath+for循环提取信息

       def info_url(self):#获取页面中的url等信息
            self.driver.current_window_handle
            # 先定位,查看是否加载元素
            WebDriverWait(self.driver, 1000).until(
                EC.presence_of_element_located(
                    (By.XPATH, "//tr[@onmouseout='this.className=rowClass']")))
            infos = self.driver.find_elements_by_xpath("//tr[@onmouseout='this.className=rowClass']")#获取标题
            some_info=[]
            self.urls = []
            self.titles = []
            for info in infos:#选择性获取你需要的信息
                title = info.find_element_by_xpath('.//a/span').get_attribute('title') #标题
                url = info.find_element_by_xpath('.//a').get_attribute('href')#链接
                address = info.find_element_by_xpath('./td[2]').text
                ways = info.find_element_by_xpath('./td[4]').text#方式
                time1 = info.find_element_by_xpath('./td[5]').text#日期
                time2 = info.find_element_by_xpath('./td[6]').text
                self.urls.append(url)
                self.titles.append(title)
    

    如何重新打开一个页面

    js = 'window.open("{}");'.format(url)
    self.driver.execute_script(js)
    

    如何切换页面句柄

    handles = self.driver.window_handles
    for handle in handles:
        if handle != self.driver.current_window_handle:
            self.driver.switch_to.window(handle)
            
            self.driver.close()
    

    如何截图

    self.driver.switch_to.window(handle)
    window_height = self.driver.get_window_size()['height']
    page_width = self.driver.execute_script('return document.documentElement.scrollWidth')  # 页面宽度
    page_height = self.driver.execute_script('return document.documentElement.scrollHeight')  # 页面高度
    self.driver.set_window_size(page_width, page_height)  # 窗口大小调整
    u = 'F:\pycharm_pracise\land\Picture\%s.png' % self.titles[t]#改到你自己创建的文件夹
    self.driver.get_screenshot_as_file(u)
    #self.driver.save_screenshot('%s.png'%self.titles[t])  # 截屏
    print('已截图......第%d张'%t)
    

    如何解决存在javascript:void的标签的查找或者点击

    aElements = self.driver.find_elements_by_tag_name("a")
    time.sleep(2)
    names = []
    for name in aElements:
        if (name.get_attribute("href") is not None and "javascript:void" in name.get_attribute("href")):
            names.append(name)
            
    #点击self.driver.execute_script('arguments[0].click();',names[-2])
    
    写代码之前构思好流畅的思路以及流程
    
    流程:
    打开页面-》输入时间-》获取一共多少页和页面中url-》解析整页的页面-》翻页
                             *            循环               *
                             *            循环               *
                             ********************************
    
  • 相关阅读:
    linux安装python3.6以后报错处理
    cent07设置服务开机自启
    python进程和线程中的两个锁
    针对Material主题怎么修改 phpstorm 代码编辑器的样式
    CentOS中zip工具的基本使用
    Anaconda退出Base环境
    微擎用户自动回复对应的链接、控制器、模板
    微擎应用模块中的参数设置对应的前端页面
    微擎设置借用权限
    微擎分配不同的用户具有不同应用的操作权限
  • 原文地址:https://www.cnblogs.com/DemoLi/p/12770342.html
Copyright © 2011-2022 走看看