无法通过请求访问AJAX url, BeautifulSoup



我正在尝试读取表的数据,这是以下网页的onclick ajax事件

如果您在页面底部单击Tabelas选项卡右侧的+号,该事件将启动。

在浏览器中使用FireBug(例如),我可以从。NET部分的XHR选项卡中获取ajax url。

url是有效的,浏览器会选择它并显示它。

我的脚本:

 import requests
 urls="http://www.hidrografico.pt/components/com_products/scripts/server/data_getestactable.php"
 headers = {
'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'
 }
 s = requests.Session()
 s.post(urls)
 content = s.post(urls, headers=headers)
 print content.content

输出:

Direct access to this file is prohibited.

所以它似乎没有直接访问url,虽然如果我在浏览器中粘贴url,我可以看到表,这是在源代码。

我不知道是我错过了什么,还是页面本身阻止了任何直接的阅读尝试。

我尝试通过主网页访问表使用BeautifulSoup(文本),然后blabla.find(类,{'id':blabla}),然后blabla. findall(),但它给出了返回

AttributeError: 'NoneType' object has no attribute 'findAll'

因为属性类'find'找不到任何东西。

我将感谢任何帮助和指导来解决这个障碍。

如果你检查POST参数,你会发现你需要发送estid=4&param=1,这将只工作,如果你有正确的cookie,你可以通过发送 get 请求到首页。

import requests

# Prepare the session that will store the cookies.
s = requests.Session()
# Get the cookies
s.get("http://www.hidrografico.pt/boias-ondografo.php")
table_url = "http://www.hidrografico.pt/components/com_products/scripts/server/data_getestactable.php"    
# Prepare the parameters
payload = { "estid": "4",
        "param": "1" 
        }
r = s.post(table_url, data=payload)
print r.text

最新更新