【问题标题】:get renderd javascript lines from website in python从 python 中的网站获取渲染的 javascript 行
【发布时间】:2018-07-18 13:49:26
【问题描述】:

我为此使用 python 3.6.6。

我正在尝试从 pycharm 网站 (https://www.jetbrains.com/pycharm/download/#section=windows) 获取 pycharm 的当前版本号。 版本号显示的很明显,但我还是看不到,因为我不知道如何正确处理java脚本。

我尝试使用 requests_html 解析它:

<li>Version: <span data-code="PCP" data-release-version=""></span></li>

java 脚本完成工作后,这部分应该如下所示:

<li>Version: <span data-code="PCP" data-release-version="">2018.1.4</span></li>

顺便说一下,这是我不工作的脚本:

from requests_html import HTMLSession

session = HTMLSession()
r = session.get('https://www.jetbrains.com/pycharm/download/#section=windows')


r.html.render()
item = r.html.find('<span data-code="PCP" data-release-version=""></span>')


print(item)

我不在乎是否会留下任何部分,我会简单地用 RegEx 过滤掉它们。我仍然从中得到的唯一结果是:

[<Element 'span' data-code='PCP' data-release-version=''>]

【问题讨论】:

    标签: python web-scraping python-3.6 python-requests-html


    【解决方案1】:

    更新:

    我自己找到了解决方案。看起来 render() 需要睡觉。我也使用 xpath 而不是搜索。

    from requests_html import HTMLSession
    
    session = HTMLSession()
    r = session.get('https://www.jetbrains.com/pycharm/download/#section=windows')
    
    
    r.html.render(sleep=0.1)
    item = r.html.xpath('/html/body/div[1]/div[2]/div/div[2]/div[1]/div[2]/ul[1]/li[1]/span/text()')
    
    print('------------------------------------------------')
    print(item)
    

    我的结果:

    ['2018.1.4']
    

    【讨论】:

    • 这帮助我解决了我的问题!知道为什么需要睡眠吗?
    • @BernardL Render 在背景中打开铬。它可能会尝试快速获取脚本内容,因此仅获取原始形式的 html。 Sleep 让 chromium 有一些时间在获取 javascript 之前加载它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多