【问题标题】:Html.render doesn't work in azure virtual machinesHtml.render 在 azure 虚拟机中不起作用
【发布时间】:2021-11-08 03:14:51
【问题描述】:

我需要进行一些网页抓取,并且我正在使用 requests_html.HTMLSession 中的渲染方法,因为该页面使用 JavaScript。当我在我的电脑上运行脚本时,它运行良好,但我需要能够在天蓝色的虚拟机中运行它,它不起作用,因为它永远不会结束并抛出这个“从浏览器引发的异常”。你能帮帮我吗?

感兴趣的代码:

from requests_html import HTMLSession

...

def get_page_content(link):
    s = HTMLSession()

    response = s.get(link)
    response.html.render(timeout=30, reload=False) #Here is where it fails.

    s.close()    

    return response.text


谢谢

【问题讨论】:

  • 您在本地 PC 和用于渲染脚本的 azure VM 中使用的浏览器是否相同?

标签: python azure web-scraping azure-virtual-machine


【解决方案1】:

建议您尝试使用此代码。

from requests_html  import HTMLSession

session  =HTMLSession()
response = session.get('https://www.cnblogs.com/pythonywy/')

print(response.html.render(keep_page=true))

async def run():
    # interactive statements 
    await r.html.page.XXX

 

try:
    session.loop.run_until_complete(run())
finally:
    session.close()

参考:https://www.codestudyblog.com/cnb11/1124173502.html

并关注这个 GITHUB 讨论:https://github.com/psf/requests-html/issues/294

【讨论】:

    猜你喜欢
    • 2019-12-13
    • 2014-04-05
    • 2016-11-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多