硒和美丽的小组更改html元素内的数字
这是我的代码
driver = webdriver.Chrome(service=chrome_driver_path)
driver.get("https://www.premierleague.com/stats/top/clubs/total_yel_card?se=20")
cards = {}
for i in range(1, 21):
path = '//*[@id="mainContent"]/div[2]/div/div[2]/div[1]/div[2]/table/tbody/tr[' + str(i) + ']/td[2]/a'
name = driver.find_element(By.XPATH, path).text
path_card = '//*[@id="mainContent"]/div[2]/div/div[2]/div[1]/div[2]/table/tbody/tr[' + str(i) + ']/td[3]'
card = driver.find_element(By.XPATH, path_card).text
cards[name] = card
,但是,卡的数量与html中的卡大不相同。 结果
cards
{'Chelsea': '1,800',
'Everton': '1,778',
'Arsenal': '1,739',
'Tottenham Hotspur': '1,705',
'Manchester United': '1,685',
'West Ham United': '1,610',
'Aston Villa': '1,572',
'Newcastle United': '1,534',
'Liverpool': '1,429',
'Manchester City': '1,409',
'Southampton': '1,298',
'Blackburn Rovers': '1,111',
'Sunderland': '1,095',
'Middlesbrough': '973',
'Leeds United': '952',
'Leicester City': '885',
'Bolton Wanderers': '845',
'Fulham': '843',
'Crystal Palace': '790',
'West Bromwich Albion': '769'}
这是我确实遇到了这个问题的 ,但数字通常与货币有关。但是,这一次似乎没有任何需要将实际数字转换为其他数字。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我快速查看了该网站,看来您的程序正在删除“所有季节”数据。
如果您在打开时仔细查看网站,则在实际应用过滤器之前,您会发现该表首先显示“所有季节”数据。
通过在声明驱动程序和前面的开始之间添加
time.sleep(1)
,这是可以解决的。这创建了另一个问题,因为词典的后半部分返回了空。我通过
这是我运行的代码:
这就是结果:
希望这有帮助!
I took a quick look at the website, and it seems that your program is pulling the "All Seasons" data.
If you take a good look at the website while opening, you see that the table first shows the "All Seasons" data, before actually applying the filter.
This was solvable with adding a
time.sleep(1)
between declaring the driver and the start of your for-loop. This created another issue, since the second half of the dictionary returned empty.I solved this by explicitly waiting for the "accept all cookies" button to appear, to then click it.
Here's the code I ran:
Here is the result:
Hope this helps!