我正在努力刮这个webpage
我想从下表中获取内容:
统计表内容显示在:div标记中,类名称为“srl tab srl tab handball playerstats sr widget sr-widget-level-0 sr handball playerstats sr normal”
我正在使用以下代码:
from selenium import webdriver
from bs4 import BeautifulSoup
link = "https://www.liquimoly-hbl.de/en/import/games/season-2020-2021/bundesliga/21--gameday--rhein-neckar-loewen---eulen-ludwigshafen/"
driver = webdriver.Chrome("path-to-my-chromedrivers")
driver.get(link)
driver.switch_to.frame("iframe-23400665")
page_source = driver.page_source
driver.close()
soup = BeautifulSoup(page_source, "html5lib")
a = soup.find("div", {"class": "srl-tabs-wrapper srl-flex-child"}).find("div", {"srl-tabs srl-flex"}).find("div", {'class': "srl-tabs-content-wrapper srl-flex-child"}).find("div", {"class": "srl-tabs-content"})
print(a.find("div", {"class": "srl-tab srl-tab-handball-playerstats sr-widget sr-widget-level-0 sr-handball-playerstats sr-normal"}))
print语句的输出为我提供了“None”值,因为网站中存在一个div标记,该标记具有相同的类名,我不知道为什么没有获得所需的输出
我必须在代码中更改什么?还是我做错了什么
编辑:
我认为从页面源获取统计表值的唯一方法是首先单击统计按钮,然后提取页面源。我不知道如何点击统计按钮
driver = webdriver.Chrome('path-to-my-chromedrivers')
driver.get(link)
driver.switch_to.frame("iframe-23400665")
page_source = driver.page_source
x = driver.find_elements_by_xpath("//div[@class='srl-tab']")
for i in x:
if i.get_attribute("data-widget") == "handball.playerstats":
print(i.get_attribute("class"))
print(i.get_attribute("data-widget"))
driver.execute_script("arguments[0].click();", i)
print(i.get_attribute("class"))
page_source_2 = driver.page_source
break
driver.close()
输出:-
srl-tab
handball.playerstats
srl-tab selected
我现在可以点击统计按钮(参见类srl-tab
更改为srl-tab selected
)
但还是没能拿到球员总积分表
您可以使用pandas库中的read_html()函数从网页中提取表格。 它可以有效地刮取网页并从页面中提取表格。您甚至可以手动生成值或直接将其保存到csv文件中
获取有关pandashere的更多信息或阅读有关该特定函数here的更多信息
希望它能帮助你的用例
转到第页,单击弹出窗口,移动到iframe,单击统计信息并等待。将源代码传递给Beautifulsoup并执行您想要的操作
进口
请尝试此代码一次 只需编辑类名和其他URL即可获得该表
我在这里留下一些屏幕截图,请参考它对我有用
Image1Image2
相关问题 更多 >
编程相关推荐