当页面需要启用JavaScript时,Python会获取URL内容 [英] Python get URL contents when page requires JavaScript enabled

查看:121
本文介绍了当页面需要启用JavaScript时,Python会获取URL内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Python获取托管在我网站上的文本文件的内容。服务器需要在您的浏览器上启用JavaScript。因此,当我运行时:

I am looking to get the contents of a text file hosted on my website using Python. The server requires JavaScript to be enabled on your browser. Therefore when I run:

    import urllib2  
    target_url = "http://09hannd.me/ai/request.txt"
    data = urllib2.urlopen(target_url) 

我收到一个html页面说启用JavaScript。
我想知道是否有一种假装启用JS的方法。

I receive a html page saying to enable JavaScript. I was wondering if there was a way of faking having JS enabled or something.

谢谢

推荐答案

Selenium 是这里的方法,但还有另一个hacky选项。

Selenium is the way to go here, but there is another "hacky" option.

根据这个答案: https://stackoverflow.com/a/26393257/2517622

import requests

url = 'http://09hannd.me/ai/request.txt'
response = requests.get(url, cookies={'__test': '2501c0bc9fd535a3dc831e57dc8b1eb0'})
print(response.content) # Output: find me a cafe nearby

这篇关于当页面需要启用JavaScript时,Python会获取URL内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆