【发布时间】:2021-12-11 13:43:57
【问题描述】:
情况
我正在尝试抓取网页以获取一些数据。 对于我的应用程序,我需要在浏览器中作为一个整体可以查看的 html 数据。
问题
但是当我抓取一些 url 时,我得到的数据无法从浏览器中查看。但在 html 代码中。那么有什么方法可以抓取只能在浏览器中查看的数据
代码
from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.chrome.service import Service
options = webdriver.ChromeOptions()
options.add_argument("--headless")
service = Service("/home/nebu/selenium_drivers/chromedriver")
URL = "https://augustasymphony.com/event/top-of-the-world/"
try:
driver = webdriver.Chrome(service = service, options = options)
driver.get(URL)
driver.implicitly_wait(2)
html_content = driver.page_source
driver.quit()
except WebDriverException:
driver.quit()
soup = BeautifulSoup(html_content)
for each in ['header','footer']:
s = soup.find(each)
if s == None:
continue
else:
s.extract()
text = soup.getText(separator=u' ')
print(text)
问题
我哪里错了? 我该如何调试呢?
【问题讨论】:
-
您能否更具体地了解抓取过程中发生的情况。因为如果它在返回的 HTML 中,那么从技术上讲,您可以收集所述数据?
-
@Kwsswart 在 html 代码中,我正在获取该数据。但我只想要在浏览器窗口中可见的数据。
-
HTML 是浏览器中可查看的内容。您能否发布一份您收到的 html 副本,我可以尝试查看其中的问题
-
通常当你抓取一个页面时,你会一次得到整个页面,然后你需要通过该页面只从整个 html 中获取你想要的数据
-
first:服务器可能会检测到
Selenium并发送不同的内容。第二:某些元素可能在 HTML 中,但可能不会显示,因为它可能使用CSS隐藏它 - 但BeautifulSoup不能使用CSS删除它。最好使用BeautifulSoup或Selenium来仅获取您真正需要的值 - 但它需要更复杂的代码。
标签: python selenium web-scraping beautifulsoup