这是我的第一篇帖子。希望澄清
我正在浏览一个网站,下面是我感兴趣的代码:
<div id="live-table">
<div class="event mobile event--summary">
<div elementtiming="SpeedCurveFRP" class="leagues--static event--leagues summary-results">
<div class="sportName tennis">
<div id="g_2_ldRHDOEp" title="Clicca per i dettagli dell'incontro!" class="event__matchevent__match--static event__match--twoLine">
...
我想获得的是最后一个id(g_2_ldRHDOEp
),下面是我使用beautifulsoup库生成的代码
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup
url = '...'
response = urllib.request.urlopen(url)
webContent = response.read()
soup = BeautifulSoup(webContent, 'html.parser')
list = []
list = soup.find_all("div")
total_id = " "
for i in list :
id = i.get('id')
total_id = total_id + "\n" + str(id)
print(total_id)
但我得到的只是
live-table
None
None
None
None
我对python和beautifulsoup都是新手,我不是seriuos程序员,我这样做只是为了好玩。 有人能回答我为什么我不能得到我想要的,也许是我如何能以更好和成功的方式做到这一点? 先谢谢你
首先,^{} 和^{} 是内置函数,所以不要将它们用作变量名
网站是动态加载的,因此
requests
不支持它。我们可以使用Selenium作为另一种刮取页面的方法安装时使用:
pip install selenium
从here下载正确的ChromeDriver
输出:
相关问题 更多 >
编程相关推荐