【发布时间】:2019-08-12 18:19:33
【问题描述】:
我一直在尝试提取网站上表格中的内容。
descriptions = []
sources = []
values = []
site = 'https://www.eia.gov/todayinenergy/prices.php' #address of the site
driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.execute_script("document.body.style.zoom='100%'")
driver.get(site)
soup_1 = bs(driver.page_source, 'lxml') #clean up the site using beautiful soup
tables = soup_1.find_all('tbody') #script of interest
print(len(tables)) #count the scripts
for table in tables:
rows = table.find_all('tr')
print(len(rows))
for row in rows:
description = row.find('td', class_='s1')
descriptions.append(descri_clean)
source = row.find('td', class_='s2')
sources.append(source_clean)
value = row.find('td', class_='d1') #find the row that gives the data
values.append(value_clean) #compile it all together
driver.close()
我一直在尝试从表格中获取干净的文本,但是提取的数据看起来像这样。
<td class="s1" rowspan="3">Crude Oil<br/> ($/barrel)</td>
虽然我想要类似“原油($/桶)”之类的东西
当我尝试时
description = row.find('td', class_='s1').text.renderContents()
descriptions.append(descri_clean)
错误出现了
AttributeError: 'NoneType' object has no attribute 'renderContents'
【问题讨论】:
-
可以提供网址吗?
-
@QHarr 我已经更新了链接。
-
你想要所有的表?
-
不是所有我只想要一些但我不知道如何只提取其中一些。
-
你想要哪些?
标签: python beautifulsoup