zoukankan      html  css  js  c++  java
  • 5.3Python解析动态页面源代码

    首先就是这一些设置一些selenium,不让他弹出浏览器,然后将etree=html.etree

    import datetime
    import json
    import threading
    import traceback
    import time
    from shlex import join
    
    from selenium import webdriver
    from selenium.webdriver import ChromeOptions
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver import Chrome
    import pymysql
    import requests
    from concurrent.futures import ThreadPoolExecutor
    from lxml import html
    
    import re
    option = ChromeOptions()
    option.add_experimental_option("excludeSwitches", ["enable-automation"])
    option.add_argument("--headless")
    option.add_argument("--disable-gpu")
    option.add_argument('window-size=1920x3000')  # 指定浏览器分辨率
    option.add_argument('--disable-gpu')  # 谷歌文档提到需要加上这个属性来规避bug
    option.add_argument('--hide-scrollbars')  # 隐藏滚动条, 应对一些特殊页面
    option.add_argument('blink-settings=imagesEnabled=false')  # 不加载图片, 提升速度
    option.add_argument('--headless')  # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
    option.binary_location = r"C:UsersLenovoAppDataLocalGoogleChromeApplicationchrome.exe"  # 手动指定使用的浏览器位置
    
    
    web = Chrome(options=option)  # 把参数配置设置到浏览器中
    
    etree = html.etree
    web.get("https://trp.autonavi.com/diagnosis/rank.do")

    之后将获取到的源代码进行re解析(因为这个获取到的代码头尾是有一些别的东西的)

    js=web.page_source
    obj1 = re.compile(r'<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">(?P<ul>.*?)</pre></body></html>', re.S)
    result1 = obj1.finditer(js)
  • 相关阅读:
    使用DBCP时发生AbstractMethodError异常
    Android NDK 环境搭建 + 测试例程
    一个关于含有显式实参的虚函数调用问题解释
    cookie和session
    hello
    python 多线程多进程
    python 网络编程
    docker 制作镜像
    docker 数据卷存储
    docker 简单原理及相关命令(镜像拉取 删除 执行容器 进入容器)
  • 原文地址:https://www.cnblogs.com/zhaoyids/p/14905312.html
Copyright © 2011-2022 走看看