【问题标题】:web scraping in python only retrieving one entrypython中的网络抓取仅检索一个条目
【发布时间】:2016-02-26 09:21:34
【问题描述】:

我正在尝试废弃 BBC 足球结果网站,以获取球队、射门、进球、卡片和事件。

我用 Python 编写脚本并使用 Beautiful soup 包。提供的代码仅检索事件中表的第一个条目。当事件表打印到屏幕上时,完整的表将所有数据都在那里。

我从中抓取的表存储在事件中:

from bs4 import BeautifulSoup
import urllib2

url = 'http://www.bbc.co.uk/sport/football/result/partial/EFBO815155?teamview=false'
inner_page = urllib2.urlopen(url).read()
soupb = BeautifulSoup(inner_page, 'lxml')    

              for incidents in soupb.find_all('table', class_="incidents-table"):
               print incidents.prettify()
               home_inc_tag = incidents.find('td', class_='incident-player-home')
               home_inc = home_inc_tag and ''.join(home_inc_tag.stripped_strings)


               type_inc_tag = incidents.find('td', 'span', class_='incident-type goal')
               type_inc = type_inc_tag and ''.join(type_inc_tag.stripped_strings)

               time_inc_tag = incidents.find('td', class_='incident-time')
               time_inc = time_inc_tag and ''.join(time_inc_tag.stripped_strings)

               away_inc_tag = incidents.find('td', class_='incident-player-away')
               away_inc = away_inc_tag and ''.join(away_inc_tag.stripped_strings)

               print home_inc, time_inc, type_inc, away_inc

在我将正则表达式添加到 URL 中以获取所有匹配详细信息之前,我现在只关注一个匹配以得到正确的结果 (EFBO815155)。

所以,incidents for 循环没有获取所有数据,只是获取表中的第一个条目。

在此先感谢,我是堆栈溢出的新手,如果这篇文章、格式等有任何问题,请告诉我。 谢谢!

【问题讨论】:

标签: python web-scraping beautifulsoup


【解决方案1】:

首先,获取事件表:

  incidentsTable = soupb.find_all('table', class_='incidents-table')[0]

然后循环遍历该表中的所有“tr”标签。

  for incidents in incidentsTable.find_all('tr'):
           # your code as it is
           print incidents.prettify()
           home_inc_tag = incidents.find('td', class_='incident-player-home')
           home_inc = home_inc_tag and ''.join(home_inc_tag.stripped_strings)
           .
           .
           .

给出输出:

Bradford Park Avenue 1-2 Boston United
None None
 2' Goal J.Rollins
 36' None C.Piergianni
N.Turner 42' None 
 50' Goal D.Southwell
C.King 60' Goal 

这与您想要的很接近。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    相关资源
    最近更新 更多