【发布时间】:2022-08-19 03:14:48
【问题描述】:
我正在尝试通过编写 Web 抓取 python 脚本并将其部署在 Azure Databrick 中,自动将 ASX (https://www.asxenergy.com.au/futures_nz) 网站的数据提取到我的数据库中。目前,我的脚本在 Visual Studio Code 中运行,但是当我尝试在 databrick 中运行它时,它崩溃了,并抛出了下面的错误。
Could not get version for google-chrome with the command: google-chrome --version || google-chrome-stable --version || google-chrome-beta --version || google-chrome-dev --version
我相信我需要简化我的代码才能在不提及我们浏览器的情况下获取表格。
我的示例代码如下:
import time
from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import pandas as pd
import sys
from datetime import datetime
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument(\'headless\')
browser = webdriver.Chrome(ChromeDriverManager().install())
#browser = webdriver.Chrome(\'C:/chromedriver\',options=options) # Optional argument, if not specified will search path.
browser.get(\'https://www.asxenergy.com.au/futures_nz\')
time.sleep(3)
html = browser.page_source
soup = BeautifulSoup(html,\'html.parser\')
market_dataset = soup.find_all(attrs={\'class\':\'market-dataset\'})
market_dataset
我尝试改用下面的代码,只使用request 包,但它失败了,因为它找不到\'market-dataset\'div class。
import time
from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import pandas as pd
import sys
from datetime import datetime
from webdriver_manager.chrome import ChromeDriverManager
URL = \"https://www.asxenergy.com.au/futures_nz\"
page = requests.get(URL)
soup = BeautifulSoup(page.content, \"html.parser\")
market_dataset = soup.findAll(\"div\",href=True,attrs={\'class\':\'market-dataset\'})
谁能帮帮我吗。
-
您可以在没有浏览器的情况下抓取原始 HTML,但该 HTML 必须包含您想要的数据。如果它是通过 Javascript 动态加载的,那么您要么需要更深入地挖掘并找出它从哪里获取数据,要么您确实需要使用无头浏览器,然后您还需要在 Azure 上安装该浏览器。
-
它使用 JavaScript 从asxenergy.com.au/futures_nz/dataset 加载数据
-
你的
findAll(\"div\",href=True, ...试图找到<div href=\"...\">但这个页面没有
标签: python html web-scraping request screen-scraping