【问题标题】:Use BeautifulSoup to obtain "View Element" code instead of "View Source" code使用 BeautifulSoup 获取“查看元素”代码而不是“查看源代码”代码
【发布时间】: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 完成生成页面元素后生成的代码中搜索。
  • 好的。在这里提供帮助的一种方法可能是thisthis tutorial。希望这会有所帮助。

标签: javascript python html beautifulsoup


【解决方案1】:

首先要了解的是,BeautifulSoupurllib2 都不是浏览器。 urllib2 只会获取/下载初始“静态”页面 - 它无法像真正的浏览器那样执行 JavaScript。因此,您将始终获得“查看页面源”内容。

要解决您的问题 - 通过selenium 启动真正的浏览器,等待页面加载,获取.page_source 并将其传递给BeautifulSoup 进行解析:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.get("http://racing4everyone.eu/2015/10/25/formula-e-201516formula-e-201516-round01-china-race/")

# wait for the page to load
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".fluid-width-video-wrapper")))

# get the page source
page_source = driver.page_source

driver.close()

# parse the HTML
soup = BeautifulSoup(page_source, "html.parser")
script = soup.find_all("script")
print(script)

这是一般方法,但您的情况有点不同 - 有一个包含视频播放器的 iframe 元素。如果要访问iframe 中的script 元素,则需要切换到它,然后获取.page_source

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Firefox()
driver.get("http://racing4everyone.eu/2015/10/25/formula-e-201516formula-e-201516-round01-china-race/")

# wait for the page to load, switch to iframe
wait = WebDriverWait(driver, 10)
frame = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "iframe[src*=video]")))
driver.switch_to.frame(frame)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".controls")))

# get the page source
page_source = driver.page_source

driver.close()

# parse the HTML
soup = BeautifulSoup(page_source, "html.parser")
script = soup.find_all("script")
print(script)

【讨论】:

  • 真的令人印象深刻。谢谢。
猜你喜欢
  • 2013-07-28
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
  • 2011-05-16
  • 2010-12-21
  • 1970-01-01
相关资源
最近更新 更多