假设您正试图从使用javascript模板(例如handlebars之类)呈现的页面获取值,那么这就是任何标准解决方案(即beautifulsoup
或requests
)所能获得的。
这是因为浏览器使用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对其进行解析。祝好运 :)