Python:为特定内容抓取 table
Python: Scraping a table for specific content
我正在尝试抓取网站上特定 table 的特定部分。
URL = https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J9775A
在网站中,有一个 HTML table 我想从中抓取,我可以做到,但是,我得到了 HTML table 的很多其他项目=35=]我不需要。如果您查看 URL,table 由多个下拉列表组成,而我只需要 "Current Releases" 列表。
检查元素让我可以使用 Screenshot
如您所见,有许多 Table 类型为 "Current_Releases" 的行,但我无法弄清楚如何只提取这些。
我正在使用 Python 3.2 和 BeautifulSoup,当然还有请求和 csv
这是我的代码:
url = "https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J9775A"
r = requests.get(url)
soup = BeautifulSoup(r.content)
table = soup.find('table', attrs={"class": "hpui-standardHrGrid-table"})
headers = [header.text for header in table.find_all('th')]
rows = []
for row in table.find_all('tr'):
rows.append([val.text.encode('utf8') for val in row.find_all('td')])
with open('c:\source\output_file.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(row for row in rows if row)
在此先感谢您的任何建议和帮助,因为我是 python
的新手
将 table.find_all('tr')
替换为 table.find_all('tr', {'releasetype':
'Current_Releases')
会发现 <tr>
的属性 releasetype
为 Current_Releases
。
查看 docs 了解更多信息。
更新:添加完整代码
import csv
import requests
from bs4 import BeautifulSoup
url = "https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J9775A"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
table = soup.find('table', attrs={"class": "hpui-standardHrGrid-table"})
headers = [header.text for header in table.find_all('th')]
rows = []
for row in table.find_all('tr', {'releasetype': 'Current_Releases'}):
item = []
for val in row.find_all('td'):
item.append(val.text.encode('utf8').strip())
rows.append(item)
with open('output_file.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(rows)
给我一个带输出的 CSV 文件
Version,Build Date,Posted Date,Notes,Size,Select
YA.16.03.0004,17-Apr-2017,24-Apr-2017,Release notes,13.5 MB,»
YA.16.02.0018,30-Mar-2017,06-Apr-2017,Release notes,12.7 MB,»
YA.16.01.0012,26-Jan-2017,01-Feb-2017,Release notes,12.5 MB,»
YA.15.18.0013,01-Sep-2016,22-Sep-2016,Release notes,11.9 MB,»
YA.15.16.0019m (Maintenance),27-Mar-2017,29-Mar-2017,Release notes,10.2 MB,»
我正在尝试抓取网站上特定 table 的特定部分。
URL = https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J9775A
在网站中,有一个 HTML table 我想从中抓取,我可以做到,但是,我得到了 HTML table 的很多其他项目=35=]我不需要。如果您查看 URL,table 由多个下拉列表组成,而我只需要 "Current Releases" 列表。
检查元素让我可以使用 Screenshot
如您所见,有许多 Table 类型为 "Current_Releases" 的行,但我无法弄清楚如何只提取这些。
我正在使用 Python 3.2 和 BeautifulSoup,当然还有请求和 csv
这是我的代码:
url = "https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J9775A"
r = requests.get(url)
soup = BeautifulSoup(r.content)
table = soup.find('table', attrs={"class": "hpui-standardHrGrid-table"})
headers = [header.text for header in table.find_all('th')]
rows = []
for row in table.find_all('tr'):
rows.append([val.text.encode('utf8') for val in row.find_all('td')])
with open('c:\source\output_file.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(row for row in rows if row)
在此先感谢您的任何建议和帮助,因为我是 python
的新手将 table.find_all('tr')
替换为 table.find_all('tr', {'releasetype':
'Current_Releases')
会发现 <tr>
的属性 releasetype
为 Current_Releases
。
查看 docs 了解更多信息。
更新:添加完整代码
import csv
import requests
from bs4 import BeautifulSoup
url = "https://h10145.www1.hpe.com/downloads/SoftwareReleases.aspx?ProductNumber=J9775A"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
table = soup.find('table', attrs={"class": "hpui-standardHrGrid-table"})
headers = [header.text for header in table.find_all('th')]
rows = []
for row in table.find_all('tr', {'releasetype': 'Current_Releases'}):
item = []
for val in row.find_all('td'):
item.append(val.text.encode('utf8').strip())
rows.append(item)
with open('output_file.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(rows)
给我一个带输出的 CSV 文件
Version,Build Date,Posted Date,Notes,Size,Select
YA.16.03.0004,17-Apr-2017,24-Apr-2017,Release notes,13.5 MB,»
YA.16.02.0018,30-Mar-2017,06-Apr-2017,Release notes,12.7 MB,»
YA.16.01.0012,26-Jan-2017,01-Feb-2017,Release notes,12.5 MB,»
YA.15.18.0013,01-Sep-2016,22-Sep-2016,Release notes,11.9 MB,»
YA.15.16.0019m (Maintenance),27-Mar-2017,29-Mar-2017,Release notes,10.2 MB,»