为什么我不能用美丽的汤刮?

时间:2019-09-23 11:55:18

标签: python beautifulsoup python-requests

我需要从该网站上抓取唯一的表格:https://core.ap.gov.in/CMDashBoard/UserInterface/eAgriculture/eAgricultureVillagewise.aspx?mandal=Agali&district=Anantapuramu

我用了漂亮的汤,但要求没有成功。你们能建议我我要去哪里了吗?

mandal_url = "https://core.ap.gov.in/CMDashBoard/UserInterface/eAgriculture/eAgricultureVillagewise.aspx?mandal=Agali&district=Anantapuramu"
r = requests.get(mandal_url, verify=False).content
soup = bs4.BeautifulSoup(r, 'lxml')
df = pd.read_html(str(soup.find('table',{"id":"gvAgricultureVillage"})))

我正在数据框中添加“找不到页面”。我不知道我要去哪里错了!

3 个答案:

答案 0 :(得分:0)

该页面可能需要某种登录。通过单击链接自己查看,我得到image shown

您需要在请求中添加Cookie或其他一些标头,以显示“已登录”。

答案 1 :(得分:0)

尝试单击您要从无效链接中抓取的链接。当我单击您提供的链接或存储在mandal_url中的链接时,两者都返回“找不到页面”页面。因此,您以正确的方式进行抓取,但您提供给 抓取工具的网址无效/不再可用。

答案 2 :(得分:0)

我无法访问该网站。但是您可以使用以下方法直接在网页上阅读表单:

dfs = pd.read_html(your_url, header=0) 

如果url需要验证,则可以通过以下方式获取表格:

r = requests.get(url_need_authentivation, auth=('myuser', 'mypasswd'))
pd.read_html(r.text, header=0)[1]

这将简化您的代码。希望对您有帮助!