【发布时间】:2019-12-28 07:44:53
【问题描述】:
我正在尝试从页面中抓取 HTML 表格。但我没有得到正确的输出...... 我的示例代码
import datetime, time
import os
from bs4 import BeautifulSoup
import requests
URL = r'https://www.data.bsee.gov/Production/OCSProduction/Default.aspx'
res = requests.get(URL)
soup = BeautifulSoup(res.text,"lxml")
table = soup.find('table',{'class':'dxsplControl'})
list_of_rows = []
for row in table.findAll('tr'):
list_of_cells = []
for cell in row.findAll(["td"]):
text = cell.text
list_of_cells.append(text)
list_of_rows.append(list_of_cells)
for item in list_of_rows:
print(' '.join(item))
我不确定这里有什么问题,因为我是 python 新手,我从网上复制了这段代码并尝试自己制作。请帮忙..谢谢
这就是我的输出文件的样子,
Year Alaska acific Gulf Total
2009 1,823,426 22,306,167 570,309,328 594,438,921
2010 1,337,999 21,707,342 566,628,383 589,673,724
2011 1,057,866 19,820,270 481,697,096 502,575,232
2012 627,108 17,678,493 464,786,485 483,092,086
2013 669,148 18,565,833 459,046,740 478,281,721
2014 625,303 18,506,540 510,467,459 529,599,302
2015 609,912 11,451,040 553,007,049 565,068,001
2016 546,340 6,142,614 585,353,426 592,042,380
2017 517,002 5,713,059 613,314,985 619,545,046
2018 498,216 4,873,812 642,155,689 647,527,717
Year Alaska Pacific Gulf Total
2009 29,609,015 41,282,897 2,451,076,806 2,521,968,718
2010 29,659,633 41,251,142 2,250,426,803 2,321,337,578
2011 37,801,877 36,591,564 1,826,593,930 1,900,987,371
2012 21,960,989 27,263,741 1,535,897,665 1,585,122,395
2013 29,293,586 27,505,401 1,328,279,728 1,385,078,715
2014 31,264,462 28,313,384 1,276,676,600 1,336,254,446
2015 32,249,585 14,808,085 1,307,390,047 1,354,447,717
2016 31,705,685 4,501,303 1,220,581,120 1,256,788,108
2017 29,056,185 3,949,957 1,078,657,857 1,111,663,999
2018 3,211,259 3,427,708 993,571,711 1,000,210,678
单个页面中总共有四个表我需要所有 4 个表
【问题讨论】:
-
我希望页面中的所有表格都作为输出(共 4 个表格)..@snakecharmerb
-
能贴出页面源代码吗?我无法访问此页面
-
@joseph Rajchwald 感谢您提供的信息,但 pd.read_html 不适用于该页面......我先尝试使用它。
-
仅供参考,它是 scrape(和 scraping、scraper、scraped)不是废品。 “废弃”的意思是像垃圾一样扔掉。
标签: python beautifulsoup