【发布时间】:2021-10-31 14:11:46
【问题描述】:
网址 - https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch
使用 selenium 我可以从上述 URL 中提取数据,但过程非常缓慢。有什么方法可以只使用请求库提取数据?
我使用 selenium 提取数据的代码 -
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome('chromedriver',options=option)
driver.get('https://finance.yahoo.com/quote/WRD.PA?p=WRD.PA&.tsrc=fin-srch')
time.sleep(5)
html_text2 = driver.page_source
soup2 = BeautifulSoup(html_text2,'lxml')
data1 = soup2.find("span" , "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").text.strip()
data2 = soup2.find("span" , "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($negativeColor)").text.strip()
wb = load_workbook('output.xlsx')
ws = wb.active
fontstyle = Font(size = "16")
ws['B9'].value = f'{data1} {data2}'
ws.cell(row = 9 , column = 2).font = fontstyle
wb.save("output.xlsx")
【问题讨论】:
-
到目前为止,您尝试或研究了什么?
-
@KlausD。 : 他已经表现出他的努力了,看这两行
data1 = soup2.find("span" , "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)").text.strip() data2 = soup2.find("span" , "Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($negativeColor)").text.strip()他们是bs4 -
首先我尝试仅将 beautifulsoup 与请求库一起使用,但出现错误 (nonetype)。然后我用了硒。但我认为它也可以通过 beautiulsoup 提取,但我不知道如何
-
@cruisepandey 不,他只展示了他的基于 Selenium 的代码。 “仅使用 beautifulsoup”这一短语具有误导性,因为它无法从服务器获取任何数据。那是 Selenium 的一部分。我猜他想使用请求或类似的。但是,一旦他提供了有关他的尝试的详细信息,我们就会知道。
-
请用您的尝试和完整的错误消息更新问题!
标签: python selenium beautifulsoup