【发布时间】:2020-01-10 22:05:17
【问题描述】:
我正在尝试从这个网站上抓取表格数据:https://www.playnj.com/atlantic-city/revenue/
然而,当我尝试打印表格时,它返回 None。有人可以帮助我吗?
这是我的代码:
from bs4 import BeautifulSoup
import requests
import pandas as pd
base_url = 'https://www.playnj.com/atlantic-city/revenue/'
resp = requests.get(base_url)
soup = BeautifulSoup(resp.text, "html.parser")
october_table = soup.find('table', {'id': 'tablepress-342-no-2'})
print(october_table)
这返回 None 我不确定为什么 - 理想情况下(也许我在这里错了) - 如果我的目标是从所有表中获取所有数据,那么使用与所有表相同的类包装器会更有效表,我会改用以下 2 行(但可能不会)。
all_tables = soup.findAll('table', {'class': 'dataTables_wrapper no-footer'})
print(all_tables)
但是,这也返回 None。在这里的任何帮助将不胜感激。
【问题讨论】:
-
您会收到
403 Forbidden的回复。您应该尝试使用所需的标题 -
@Sers 类似于 headers= {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'} resp = requests.get(url, headers=headers) ?
-
是的,在开始时使用带有
User-Agent的标头。您还可以显示resp.text以查看您是否没有收到机器人警告。
标签: python web-scraping beautifulsoup python-requests