您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用Python检索动态html内容的值

如何使用Python检索动态html内容的值

假设您正试图从使用javascript模板(例如handlebars之类)呈现的页面获取值,那么这就是任何标准解决方案(即beautifulsouprequests)所能获得的。

这是因为浏览器使用javascript更改了接收到的内容并创建了新的DOM元素。urllib将像浏览器一样执行请求部分,而不是模板呈现部分。本文讨论了三种主要解决方案:

从您的评论看来,它是一个由把手驱动的网站。这个答案给出了一个很好的代码示例,可能会有用:

from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://eve-central.com/home/quicklook.html?typeid=34')

html = driver.page_source
soup = BeautifulSoup(html)

# check out the docs for the kinds of things you can do with 'find_all'
# this (untested) snippet should find tags with a specific class ID
# see: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class
for tag in soup.find_all("a", class_="my_class"):
    print tag.text

硒基本上是从您的浏览器获取呈现的HTML,然后您可以使用来自page_source属性的BeautifulSoup对其进行解析。祝好运 :)

python 2022/1/1 18:14:06 有527人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶