简体   繁体   English

Python初学者:如何从网站表中的选定行中提取数据

[英]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)

This is the code for extracting the data from the website after executing code output is similar to this: Conroe 0.12 58 45 28 15.76 0.00 4.70 6.06 这是在执行代码输出之后用于从网站提取数据的代码,类似于:Conroe 0.12 58 45 28 15.76 0.00 4.70 6.06

Huntsville 0.10 56 41 27 16.21 0.00 2.10 3.57 亨茨维尔0.10 56 41 27 16.21 0.00 2.10 3.57

Overton 0.12 53 35 42 16.34 0.00 7.52 16.89 欧顿0.12 53 35 42 16.34 0.00 7.52 16.89

But I need the data of only one city.. like this: 但是我只需要一个城市的数据..像这样:

Conroe 0.12 58 45 28 15.76 0.00 4.70 6.06 康罗0.12 58 45 28 15.76 0.00 4.70 6.06

I'm not sure about what you are asking here, but my guess is, you are trying to extract the text from a table cell. 我不确定您在这里要问什么,但是我的猜测是,您正在尝试从表单元格中提取text

Have you tried print(gdata.text) 您是否尝试过print(gdata.text)

EDIT 1 编辑1

If the CSS selectors are same for all the cells, and thinking that there might be more than 2 lines of extracted data, and the desired data can be anywhere on the table; 如果所有单元格的CSS选择器都相同,并且认为提取的数据可能超过两行,则所需的数据可以在表中的任何位置; I suggest extract them all to a list and than search for Conroe As; 我建议将它们全部提取到列表中,然后搜索Conroe As;

for item in List1:
    if item.startswith('Conroe'):
        print(item)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM