当前位置: 首页 > 知识库问答 >
问题:

不一致的表数据刮取

归鹤龄
2023-03-14

我正在尝试从此网页的一个表中提取表数据。但是,当我尝试从每个表行提取表数据时,似乎无法从每一行获取数据。我检测到的一种模式是,我无法看到存在图像的行的表数据。有没有其他的方法,我仍然可以刮我想要的数据(职位,家乡,职级等),特别是当涉及到那些图片存在的时候?

# We are unable to get the table data row for individuals that have a picture

rows = soup.find_all('tr')

# This is the data for each table row

for row in rows:
    print(row.text)

我已经能够使用“div”类获得播放器名称,但我不认为我能够将它用于其他列中的数据。

# The name of the player is going to be our first column. So let's make a list of the names for tr in soup.find_all('tr'):

name = soup.find_all('div', {'class':'name'})
                    
# Empty list to put our player name in.
players = []
for person in name:
# This line is used to remove the below text that was also included wth the names
    person = person.text.replace("Video | Scouts Report","")
    players.append(person)
                    
# We see that the length of this list is 100. This means that we extraced the names correctly
len(players)

共有1个答案

况经纬
2023-03-14

我认为更简单的方法是直接使用pd.read_html()在pandas的dataframe中读取它,它将立即返回来自URL的所有表的(长度1)列表:

url = r'http://www.espn.com/college-sports/basketball/recruiting/playerrankings/_/view/espnu100/sort/rank/class/2019'
dfs = pd.read_html(url, header=0)
dfs[0].head()
#   RK                                 PLAYER POS  \
#0   1     James WisemanVideo | Scouts Report   C   
#1   2      Cole AnthonyVideo | Scouts Report  PG   
#2   3  Vernon Carey Jr.Video | Scouts Report   C   
#3   4    Isaiah StewartVideo | Scouts Report   C   
#4   5   Anthony EdwardsVideo | Scouts Report  SG   
#
#                                     HOMETOWN      HT   WT  STARS  GRADE  \
#0                 Memphis, TNEast High School   7'0''  230    NaN     97   
#1               Briarwood, NYOak Hill Academy   6'3''  185    NaN     97   
#2  Southwest Ranches, FLNSU University School  6'10''  275    NaN     97   
#3              Rochester, NYLa Lumiere School   6'9''  245    NaN     97   
#4               Atlanta, GAHoly Spirit School   6'4''  205    NaN     97   
#
#                          SCHOOL  
#0                  MemphisSigned  
#1                           List  
#2        DukeCommitted12/06/2018  
#3  WashingtonCommitted01/20/2019  
#4     GeorgiaCommitted02/11/2019 

当然,你将不得不做一些清理,但我认为这会比把所有东西都读成清单效率高得多。

 类似资料:
  • 我对使用python有点陌生,我接到了一个任务,需要从表中抓取数据。我也不太懂html。我以前从来没有这样做过,花了几天时间研究各种刮桌子的方法。不幸的是,所有的例子都是一个看起来比我所处理的更简单的网页布局。我尝试了很多不同的方法,但没有一种方法允许我选择所需的表数据。 下面网页底部的“每日水位”选项卡下的表怎么刮? url=https://apps.wrd.state.or.us/apps/g

  • 我想用英超网站上的统计数据来做一个班级项目。这是网站:https://www.premierleague.com/stats/top/players/goals

  • Oracle XE 11。一个非常简单的连接查询给了我以下错误: 表: 产品描述为CLOB。 SQL SQL 查询: 第1行的错误:ORA-00932:数据类型不一致:预期-得到CLOB 如果我删除t0。从选择名称,它将工作。奇怪的 谢谢。

  • 在调试我的应用程序时,我注意到我的RecycerView显示与提供的数据不一致,即。 > 如果我设置了一个闹钟(RecycerView中的TextView设置了日期),然后滚动我的RecycerView,日期显示在错误的位置。例如,如果我在第4项设置了日期,那么由于某种原因,第3项也设置了日期 我已经查看了文档,但不确定如何相应地修补。你能帮我吗? 我的OnBindViewholder:

  • 我正在收集COVID疫苗接种数据,并尝试使用Python从这个站点中删除疫苗编号(第一个--“接受1剂疫苗的人”)。我尝试使用BeautifulSoup提取HTML,然后通过XPath或属性进行搜索。当然,首先,我使用BeautifulSoup解析页面: 但是这个HTML输出看起来与页面上的实际HTML完全不一样--相反,它只是一行又一行的如下内容: 我不是很确定这里发生了什么--我尝试了几种不同

  • 我想更新xml中的一个值 更新trb1_sub_errs set error_status='ready_to_recycle',general_data_c=updatexml(xmltype(general_data_c),'//data/cmheadertransaction/transactionrsn/text()','creq'),其中sub_trx_id=4678604091;更新t