使用美丽汤获取“视图元素”代码,而不是“查看源代码”代码
Use BeautifulSoup to obtain "View Element" code instead of "View Source" code
我使用以下代码从网页获取所有<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搜索。
我希望它这样做,因为我真正感兴趣的代码段是元素代码而不是源代码。
首先要了解的是,BeautifulSoup
和 urllib2
都不是浏览器。 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)
相关文章:
- 在JS或jQuery或任何客户端脚本的帮助下,通过查看源代码读取源代码
- 如何在查看源代码:页面中运行 Tampermonkey
- 只能在本地打开代码时保存画布,而不能在联机查看时保存
- 在D3插件sankey.js和html代码之间定义源代码
- BootstrapValidation对我不起作用,请查看此代码
- 如何使用Html敏捷包获得具有实际源代码的javascript代码
- 使用美丽汤获取“视图元素”代码,而不是“查看源代码”代码
- 有没有办法用 AngularJS 写评论,以便在查看源代码时不可见它们
- 代码点火器查看输入问题
- 如何查看“”之间的代码
- 添加查看源代码按钮
- php 页面呈现正确,但“查看源代码”未显示正确的 HTML
- 如何从“检查元素”和“查看源代码”选项中隐藏HTML和Javascript代码
- 如果右键单击网站并查看源代码,我的json数据也会显示出来.我将如何防止它
- 查看 jQuery 源代码,如何在伪代码中实现此缓动函数
- 如何访问仅在浏览器中可见的代码's "查看源代码"
- 在表>中添加新行后查看源代码
- 禁用右键单击、打印和查看源代码会使按键P和U不起作用
- 如何将“查看页面源代码”代码复制到新文件中
- 在开发人员窗口中显示内联样式元素,但在查看源代码时不显示