【问题标题】:Webscraping : How to parse this kind of contents in Python?Webscraping:如何在 Python 中解析这种内容?
【发布时间】:2020-05-20 15:32:32
【问题描述】:

我正在做一个网页抓取项目

当我运行我的代码时:

url = myurl

session = requests.session()
response = session.get(url)
print(response.content)

response.content 如下所示:

<html><head><meta charset="utf-8"><script>function i700(){}i700.F20=function (){return typeof i700.O20.p60==='function'?i700.O20.p60.apply(i700.O20,arguments):i700.O20.p60;};i700.X70=function (){return typeof i700.v70.p60.............................

使用 Firefox 开发工具检查源网页,我找到了我需要的数据。

【问题讨论】:

    标签: python html web-scraping request


    【解决方案1】:

    您显示的响应似乎没有经过 gzip 压缩; response.content 将以 binary byte-string 的形式返回响应,这可能不是您想要的。

    为了获得纯文本的响应,您需要使用response.text。从那里,您应该能够使用 string.find() 在字符串中搜索您想要的元素。

    来源:requests documentation

    【讨论】:

    • 感谢 Drew,我尝试了您的解决方案,但 response.text 产生与 response.content 相同的输出。这个源似乎不是一个二进制字节串,它看起来像一个压缩的 javascript 函数。当我使用标准浏览器访问该站点时,“源 html”看起来不错。当我尝试从请求库或 Selenium 中获取它时,来源看起来很奇怪。
    【解决方案2】:

    经过一些研究,我找到了解决方案。 我注意到我的目标网站可以将 Selenium 检测为机器人,即使没有应用自动化。

    所以,为了在不被检测到的情况下访问这种网页,我找到了一个使用 ChromeOptions() 类添加一些参数的解决方案:

    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"]) 
    options.add_experimental_option('useAutomationExtension', False) 
    

    来源: Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      相关资源
      最近更新 更多