python-3.x - Selenium not getting different URLs
问题描述
I'm using Selenium in Python to scrape a site that loads Javascript Here's my code: [https://gist.github.com/elliotmartin/f9cb5021655f913f250b08f39a14dc9d][1]
For some reason as I loop over different URLs the get_boards function is returning the exact same results.
For example: This URL:
Gets:
{'FAST44': '1', 'Mesmile': '2', 'Pizza': '3', 'Stacker': '4', 'Jackpot': '5', 'Gavin': '6', 'Monsanto': '7', 'VictorFalcon': '8', 'Cantelope': '9', 'Rozz': '10', 'molino': '11', 'Eddie': '12', 'SwitchSSB': '13', 'Rey': '14', 'wabeka': '15', 'Enrico': '16', 'TheRabbin': '17', 'Jalexander': '18', 'Itim': '19', 'Jay': '20', 'DuVlad': '21', 'Staz': '22', 'BanditKeith': '23', 'Akatsu': '24', 'Montius': '25'}
And this URL:
Also gets:
{'FAST44': '1', 'Mesmile': '2', 'Pizza': '3', 'Stacker': '4', 'Jackpot': '5', 'Gavin': '6', 'Monsanto': '7', 'VictorFalcon': '8', 'Cantelope': '9', 'Rozz': '10', 'molino': '11', 'Eddie': '12', 'SwitchSSB': '13', 'Rey': '14', 'wabeka': '15', 'Enrico': '16', 'TheRabbin': '17', 'Jalexander': '18', 'Itim': '19', 'Jay': '20', 'DuVlad': '21', 'Staz': '22', 'BanditKeith': '23', 'Akatsu': '24', 'Montius': '25'}
But none of those values are even present anywhere in the HTML loaded by the Javascript on the second URL.
So selenium must not be reloading the new URL? I'm very new to Selenium, so I think that's where my issue lies.
[
1]: https://gist.github.com/elliotmartin/f9cb5021655f913f250b08f39a14dc9d
解决方案
Try it like this:
driver.get('https://playhearthstone.com/en-us/api/community/leaderboardsData?region=US&leaderboardId=STD&seasonId=73')
rows = driver.execute_script("""
return JSON.parse(document.body.innerText).leaderboard.rows.reduce(function(acc, o){
acc[o.accountid] = o.rank
return acc
}, {})
""")
I'm just getting the api data from the endpoint and returning it to python
推荐阅读
- docker-compose - Dockerfile 找不到我的文件退出代码 1 Dockerfile 在同一个目录中找不到文件
- javascript - 如何在 page.evaluate 中使用页面?
- javascript - 如何让左侧或右侧部分暂时保持粘性?
- angular - 将数据保存在 observable (ionic 4)
- python - python脚本从另一个csv更新csv中的现有列值
- mysql - Mysql root auth_socket 清空密码
- r - 用空白替换连续运行中的重复值
- html - 角度 DIV 上的动态访问键
- r - 将字符从列名的开头移动到列名的结尾
- google-apps-script - 在 google 脚本中将内容从正文添加到 gmail 附件