scraping different table with same classes with beautifulsoup,python

天大地大妈咪最大 提交于 2019-12-11 05:23: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!!


回答1:


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']
...


来源:https://stackoverflow.com/questions/27485854/scraping-different-table-with-same-classes-with-beautifulsoup-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!