【发布时间】:2016-07-07 21:33:39
【问题描述】:
我正在使用以下代码从网页中获取所有<script>...</script> 内容(参见代码中的 url):
import urllib2
from bs4 import BeautifulSoup
import re
import imp
url = "http://racing4everyone.eu/2015/10/25/formula-e-201516formula-e-201516-round01-china-race/"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
script = soup.find_all("script")
print script #just to check the output of script
但是,BeautifulSoup 在网页的源代码(Chrome 中的 Ctrl+U)内进行搜索。但是,我想在网页的元素代码(chrome 中的 Ctrl+Shift+I)中进行 BeautifulSoup 搜索。
我希望它这样做,因为我真正感兴趣的代码是元素代码而不是源代码。
【问题讨论】:
-
您的意思是要在脚本标签中的代码中查询?
-
是的,这是一个选项。我还发现我将需要使用
json而不是BS,因为我想在javascript 执行其操作后生成的代码中进行查询。但我仍然不知道该怎么做。但是,我也可以只获取<script>的内容,然后使用json.loads将其转换为python dic。 -
好的。假设您想要第一个
script标签,您可以使用contents获取代码,然后使用正则表达式等进行查询。看看如果您将最后一行更改为script[0].contents会发生什么 -
这仍然在页面的源代码中搜索,我想在 javascript 完成生成页面元素后生成的代码中搜索。
-
好的。在这里提供帮助的一种方法可能是this 和this tutorial。希望这会有所帮助。
标签: javascript python html beautifulsoup