用beautifulsoup,python把不同的表与相同的类拼凑起来 [英] scraping different table with same classes with beautifulsoup,python

查看:91
本文介绍了用beautifulsoup,python把不同的表与相同的类拼凑起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用美丽的汤和python,从本网站提取所有的赔率。

http://www.sportstats.com/soccer/italy/serie-a -2013-2014 / sampdoria-napoli-zZAT2c14 /#赔率/ 1X2 / s3

它们被分成不同的表格,取决于它们是什么类型。



例如:div id =betType_1_2下的第一个表格表示1X2类型为全职的赔率



我试图搜索所有class =赔率,但它也会返回其他表的赔率。
有没有人想过如何提取,然后通过其div id每次只刮一张表?然后,我将能够搜索class =odds并获取我需要的数据。
谢谢大家,对不起我的英文不好!

解决方案

您可以使用 CSS选择器 到达所需的 div

  from bs4导入BeautifulSoup 
导入请求


url =http://www.sportstats.com/soccer/italy/serie-a-2013-2014/sampdoria-napoli-zZAT2c14/?block= 3
汤= BeautifulSoup(requests.get(url).content)

id_ =betType_1_2
for soup.select('div#{id} table .oddsTable tr'.format(id = id _))[1:-1]:
print [td.text for item('td')]

打印:

  [u'bwin',u '3.20',u'3.75',u'2.05'] 
[u'FortunaWin',u'3.30',u'3.40',u'2.10']
[u'Unibet',u '3.45',u'3.50',u'2.10']
[u'Betclic',u'3.40',u'3.60 ',u'2.00']
[u'Expekt',u'3.40',u'3.60',u'2.00']
[u'Betsson',u'3.50',u'3.35 ',u'2.10']
[u'Betsafe',u'3.55',u'3.40',u'2.11']
[u'10Bet',u'3.40',u'3.45 ',u'2.10']
...


i'm trying to extract,using beautiful soup and python,all the odds from this website

http://www.sportstats.com/soccer/italy/serie-a-2013-2014/sampdoria-napoli-zZAT2c14/#odds/1X2/s3

they are divided in different tables depending on wich type do they are.

Ex:the first table under the div id="betType_1_2" represents odds of type 1X2 of "full time"

I tried to search for all class="odds" but it return also odds from others tables. Have anyone idea on how to extract and then scrape only one table per time by its "div id"?Then i would be able to search for class="odds" and obtain the data i need. Thank you all and sorry for my bad english!!

解决方案

You can use CSS selectors to get to the table rows in the desired div:

from bs4 import BeautifulSoup
import requests


url = "http://www.sportstats.com/soccer/italy/serie-a-2013-2014/sampdoria-napoli-zZAT2c14/?block=3"
soup = BeautifulSoup(requests.get(url).content)

id_ = "betType_1_2"
for item in soup.select('div#{id} table.oddsTable tr'.format(id=id_))[1:-1]:
    print [td.text for td in item('td')]

Prints:

[u'bwin', u'3.20', u'3.75', u'2.05']
[u'FortunaWin', u'3.30', u'3.40', u'2.10']
[u'Unibet', u'3.45', u'3.50', u'2.10']
[u'Betclic', u'3.40', u'3.60', u'2.00']
[u'Expekt', u'3.40', u'3.60', u'2.00']
[u'Betsson', u'3.50', u'3.35', u'2.10']
[u'Betsafe', u'3.55', u'3.40', u'2.11']
[u'10Bet', u'3.40', u'3.45', u'2.10']
...

这篇关于用beautifulsoup,python把不同的表与相同的类拼凑起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆