繁体   English   中英

无法使用网站上的 BeautifulSoup 抓取表格数据

[英]Not able to scrape table data using BeautifulSoup from a website

我正在按照在线教程 ( https://www.analyticsvidhya.com/blog/2015/10/beginner-guide-web-scraping-beautiful-soup-python/ ) 进行网络抓取 html 表。 当我按照教程进行操作时,我能够抓取表格数据,但是当我尝试从中抓取数据时( https://www.masslottery.com/games/lottery/search/results-history.html?game_id= 15&mode=2&selected_date=2019-03-04&x=12&y=11 ) 网站我无法这样做。

我之前尝试过使用 scrapy 但得到了相同的结果。

这是我使用的代码。

import urllib.request

wiki = "https://www.masslottery.com/games/lottery/search/results-history.html?game_id=15&mode=2&selected_date=2019-03-04&x=12&y=11"
page = urllib.request.urlopen(wiki)
from bs4 import BeautifulSoup
soup = BeautifulSoup(page, "lxml")


all_tables=soup.find_all('table')


right_table=soup.find('table', class_='zebra-body-only')
print(right_table)

这是我在终端上运行这段代码时得到的

<table cellspacing="0" class="zebra-body-only">
<tbody id="target-area">
</tbody>
</table>

虽然当我使用谷歌浏览器检查大众彩票的网站时,这就是我所看到的

<table cellspacing="0" class="zebra-body-only"                                  <tbody id="target-area">
<tr class="odd">
<th>Draw #</th>
<th>Draw Date</th>
<th>Winning Number</th>
<th>Bonus</th>
</tr>
<tr><td>2107238</td>
<td>03/04/2019</td>
<td>01-04-05-16-23-24-27-32-34-41-42-44-47-49-52-55-63-65-67-78</td><td>No Bonus</td>
</tr>
<tr class="odd">
<td>2107239</td>
<td>03/04/2019</td>
<td>04-05-11-15-19-20-23-24-25-28-41-45-52-63-64-68-71-72-73-76</td><td>4x</td>
</tr> 
....(And so on)

我希望能够从此表中提取数据。

发生这种情况是因为该网站进行了另一次调用以加载结果。 初始链接仅加载页面而不加载结果。 使用 chrome 开发工具检查请求,您将能够找出需要复制以获得结果的请求。

这意味着要获得结果,您只需调用上面提到的请求,而不必调用网页。

幸运的是,您必须调用的端点已经采用了良好的 JSON 格式。

GET https://www.masslottery.com/data/json/search/dailygames/history/15/201903.json?_=1555083561238

我假设1555083561238是时间戳。

该页面是动态的,因此它会在您发出请求后呈现。 您可以 a) 使用 JC1 的解决方案并访问 json 响应。 或者你可以使用 Seleneium 来模拟打开浏览器,渲染页面,然后抓取表格:

from bs4 import BeautifulSoup
from selenium import webdriver


url = 'https://www.masslottery.com/games/lottery/search/results-history.html?game_id=15&mode=2&selected_date=2019-03-04&x=12&y=11'  

driver = webdriver.Chrome()
driver.get(url)
page = driver.page_source

soup = BeautifulSoup(page, "lxml")

all_tables=soup.find_all('table')


right_table=soup.find('table', class_='zebra-body-only')

另外作为旁注:通常如果我看到<table>标签,我会让 Pandas 为我完成工作(注意,我无法访问该站点,因此无法测试这些):

import pandas as pd
from selenium import webdriver


url = 'https://www.masslottery.com/games/lottery/search/results-history.html?game_id=15&mode=2&selected_date=2019-03-04&x=12&y=11'  

driver = webdriver.Chrome()
driver.get(url)
page = driver.page_source

# will return a list of dataframes
tables = pd.read_html(page)

# chose the dataframe you want from the list by it's position
df = tables[0]

是的,我会把你得到的数据保存在一个文件中,看看你要找的东西是否真的在那里。 用 open('stuff.html','w') 作为 f: f.write(response.text)

unicode,尝试: import codecs codecs.open(fp,'w','utf-8') as f:

如果你没有看到你在那里寻找什么,你将不得不找出正确的 url 来加载,检查 chrome 开发人员选项这通常很难

简单的方法是使用 selenium 确保你等到你要找的东西出现在页面上(这是动态的)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM