如何从Python中提取日期部分

2024-10-01 19:31:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过web刮取此网站:https://www.reuters.com/companies/tsla.oq/financials/income-statement-quarterly

我正在使用Python,除了日期部分之外,所有内容都可以删除。。。i、 e.我不能刮“6月30日-20日”。我试着

from requests import get
from bs4 import BeautifulSoup
url = 'https://www.reuters.com/companies/tsla.oq/financials/income-statement-quarterly'
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
table = html_soup.find_all('div', class_ = 'tables-container')
table[0].thead.tr.find('time', class_ = 'TextLabel__text-label___3oCVw TextLabel__black___2FN-Z TextLabel__medium___t9PWg').text

但它显示空白。。。你能帮帮我吗?我们将不胜感激


Tags: textfromhttpscomhtmlwwwstatementcompanies
1条回答
网友
1楼 · 发布于 2024-10-01 19:31:15

您无法使用动态添加数据的请求(使用javascript)从网站获取数据。 您需要使用selenium来实现这一点

请参阅此代码:

from selenium import webdriver
from bs4 import BeautifulSoup
DRIVER_PATH="Your selenium chrome driver path"
url = 'https://www.reuters.com/companies/tsla.oq/financials/income-statement-quarterly'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
driver.get(url)
html_soup = BeautifulSoup(driver.page_source, 'html.parser')
table = html_soup.find_all('div', class_ = 'tables-container')
driver.quit()
print(table[0].thead.tr.find('time', class_ = 'TextLabel__text-label___3oCVw TextLabel__black___2FN-Z TextLabel__medium___t9PWg').text)

相关问题 更多 >

    热门问题