我试图从网站上获取json文件。我试过BeautifulSoup库但是空了。
在DevTools中并非空,并且有一个示例Jquery代码:
$('#domtableweek').dataTable({
"processing": true,
"serverSide": true,
"bSort": false,
"bLengthChange": false,
"pageLength": 10,
ajax: {
url: '/data/mal-domains-data',
type: 'POST',
data: {'csrfmiddlewaretoken': 'yafh4aGjo0S6SeTci6qv2PzlcEuTW7E8', 'time':'7-Day'}
},
"dataSource": ''
});
所以我尝试了这段代码
import requests
csrf = 'some_csrf_token'
cookie = 'some_cookie_id'
data = dict({'csrfmiddlewaretoken': 'yafh4aGjo0S6SeTci6qv2PzlcEuTW7E8', 'time':'7-Day'})
url = 'https://minotr.net/data/mal-domains'
headers = {'Cookie':cookie}
r = requests.post(url, data=data, headers=headers)
print r.text
结果是:
<p>You are seeing this message because this HTTPS site requires a 'Referer header' to be sent by your Web browser, but none was sent. This header is required for security reasons, to ensure that your browser is not being hijacked by third parties.</p>
<p>If you have configured your browser to disable 'Referer' headers, please re-enable them, at least for this site, or for HTTPS connections, or for 'same-origin' requests.</p>
`
你能帮帮我吗。
答案 0 :(得分:0)
所以问题是你需要设置一个&#34; Referer&#34;标题在这里。
headers = {'Cookie':cookie, 'Referer':'https://google.com/'}
试试这个
要获得JSON,您必须使用.json()方法。
r = requests.post(url, data=data, headers=headers).json()