如何使用Python检索动态html内容的值
我正在使用Python3,并试图从一个网站检索数据。但是,此数据是动态加载的,我现在的代码不起作用:如何使用Python检索动态html内容的值,python,html,templates,urllib,Python,Html,Templates,Urllib,我正在使用Python3,并试图从一个网站检索数据。但是,此数据是动态加载的,我现在的代码不起作用: url = eveCentralBaseURL + str(mineral) print("URL : %s" % url); response = request.urlopen(url) data = str(response.read(10000)) data = data.replace("\\n", "\n") print(data) 当我试图找到一个特定的值时,我会找到一个模板
url = eveCentralBaseURL + str(mineral)
print("URL : %s" % url);
response = request.urlopen(url)
data = str(response.read(10000))
data = data.replace("\\n", "\n")
print(data)
当我试图找到一个特定的值时,我会找到一个模板,例如“{{formatPrice median}}”,而不是“4.48”
如何使其能够检索值而不是占位符文本
编辑:是我试图从中提取信息的特定页面。我试图得到“中值”,它使用模板{{formatPrice median}
编辑2:我已经安装并设置了使用Selenium和BeautifulSoup的程序
我现在的代码是:
from bs4 import BeautifulSoup
from selenium import webdriver
#...
driver = webdriver.Firefox()
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html)
print "Finding..."
for tag in soup.find_all('formatPrice median'):
print tag.text
是程序执行时的屏幕截图。不幸的是,它似乎找不到任何指定了“formatPrice median”的内容。假设您试图从使用javascript模板呈现的页面中获取值(例如类似的内容),那么这就是任何标准解决方案(即
美化组
或请求
).
这是因为浏览器使用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
基本上,selenium从浏览器中获取呈现的HTML,然后您可以使用
page\u source
属性中的BeautifulSoup对其进行解析。祝你好运:)我用的是硒+铬
`from selenium import webdriver
from selenium.webdriver.chrome.options import Options
url = "www.sitetotarget.com"
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')`
构建另一个答案,但更完整
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless') #background task; don't open a window
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')#I copied this, so IDK?
options.add_argument('--disable-dev-shm-usage')#this too
driver.get(url)# set browser to use this page
time.sleep(6) # let the scripts load
html = driver.page_source #copy from chrome process to your python instance
driver.quit()
mac+chrome的安装:
pip install selenium
brew cask install chromedriver
brew cask install google-chrome
您在浏览器中访问URL时是否获得模板标记?编辑:另外,模板是如何呈现的。如果您使用的是javascript模板引擎(例如把手),这可能意味着您将在响应中获得模板标记。重新编辑2-这只是一个新问题。。。无论如何,我认为您需要查看find_all的文档,因为find_all字符串无效。我将在下面更新一些更接近您需要的内容。干杯!我试着使用soup.findall(True)来获取所有标签,我需要的信息就在那里!这只是找到我需要搜索哪个标签才能获得信息的问题。谢谢你的帮助。我对网络语言或基于网络的编程经验很少,但如果有帮助的话,我会链接我试图解析数据的网站。我也会开始调查请求并美化群。我看过这个网站——它几次差点坏了我的电脑加载:)是的,如果你是Chrome点击F12,如果你进入“网络”选项卡您将看到
主干
,下划线
和把手
都已加载。我认为您必须采用selenium
方法。我将用一些示例代码进行编辑再次感谢。我已经尝试了您推荐的内容并更新了我的帖子。:)在服务器上使用的最佳解决方案是什么?在服务器(而不是本地计算机)上使用selenium
是否是一种良好的做法@威尔·哈特