如何从Python中提取日期部分

如何从Python中提取日期部分,python,html,date,web-scraping,beautifulsoup,Python,Html,Date,Web Scraping,Beautifulsoup,我正在尝试通过web刮取此网站: 我正在使用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 =

我正在尝试通过web刮取此网站:

我正在使用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

但它显示空白。。。你能帮帮我吗?非常感谢。

您无法使用动态添加数据的请求(使用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)

您无法使用动态添加数据的请求(使用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)

非常感谢您的回复。但我不知道什么是“你的selenium chrome驱动程序路径”。你能告诉我怎么走这条路吗?仅供参考,我正在使用Google Colab运行脚本。@stevekim您好,我的意思是您需要从当前的chrome版本下载selenium chrome webdriver。否则它将抛出一个错误。将路径添加到驱动程序路径中的exe。既然你是在谷歌colab上做的,回答这个问题应该会对你有所帮助。非常感谢你的回复。但我不知道什么是“你的selenium chrome驱动程序路径”。你能告诉我怎么走这条路吗?仅供参考,我正在使用Google Colab运行脚本。@stevekim您好,我的意思是您需要从当前的chrome版本下载selenium chrome webdriver。否则它将抛出一个错误。将路径添加到驱动程序路径中的exe。既然你是在谷歌colab上做的,回答这个问题应该会对你有所帮助。