Python 初学者:如何从网站的选定行中提取数据 table
Python Beginner: How to Extract data from selected row from a website table
import urllib.request
from bs4 import BeautifulSoup
url = ('http://texaset.tamu.edu/')
page = urllib.request.urlopen(url).read()
soup = BeautifulSoup(page)
#table = soup.find_all('table')
gdata = soup.find_all('td',{"class":"Data"})
for item in gdata:
print(item.text)
这是从网站中提取数据的代码
执行代码后输出类似这样:
康罗
0.12
58
45
28
15.76
0.00
4.70
6.06
亨茨维尔
0.10
56
41
27
16.21
0.00
2.10
3.57
奥弗顿
0.12
53
35
42
16.34
0.00
7.52
16.89
但是我只需要一个城市的数据..像这样:
康罗
0.12
58
45
28
15.76
0.00
4.70
6.06
我不确定你在这里问什么,但我猜你正试图从 table 单元格中提取 text
。
你试过了吗print(gdata.text)
编辑 1
如果所有单元格的CSS选择器都相同,并且认为提取的数据可能超过2行,并且所需数据可以在table上的任何位置;我建议将它们全部提取到列表中,而不是搜索 Conroe
As;
for item in List1:
if item.startswith('Conroe'):
print(item)
import urllib.request
from bs4 import BeautifulSoup
url = ('http://texaset.tamu.edu/')
page = urllib.request.urlopen(url).read()
soup = BeautifulSoup(page)
#table = soup.find_all('table')
gdata = soup.find_all('td',{"class":"Data"})
for item in gdata:
print(item.text)
这是从网站中提取数据的代码 执行代码后输出类似这样: 康罗 0.12 58 45 28 15.76 0.00 4.70 6.06
亨茨维尔 0.10 56 41 27 16.21 0.00 2.10 3.57
奥弗顿 0.12 53 35 42 16.34 0.00 7.52 16.89
但是我只需要一个城市的数据..像这样:
康罗 0.12 58 45 28 15.76 0.00 4.70 6.06
我不确定你在这里问什么,但我猜你正试图从 table 单元格中提取 text
。
你试过了吗print(gdata.text)
编辑 1
如果所有单元格的CSS选择器都相同,并且认为提取的数据可能超过2行,并且所需数据可以在table上的任何位置;我建议将它们全部提取到列表中,而不是搜索 Conroe
As;
for item in List1:
if item.startswith('Conroe'):
print(item)