2016-03-15 81 views
0

我試圖建立一個刮板趕上聯賽名稱和odds..Been能趕上的可能性,但匹配兩個似乎不work..here是我的代碼:美麗的湯刮不佳建表

import requests 
from bs4 import BeautifulSoup 

r = requests.get("http://www.elitebetkenya.com/coupon.php") 
soup = BeautifulSoup(r.content) 

for i in soup.findAll("tr"): 
    tds = i.findAll("td") 
    fixture = soup.findAll("tr", { "class" : "fixture" }) 

    try: 
     if len(tds[0].text) != 0 : 

      print " Bet-type: %s, Choice: %s, Match code: %s, 1: %s, 0:  %s" % \ 
      (tds[0].text, tds[1].text, tds[2].text,tds[3].text, tds[4].text) 
except: 
    pass 

回答

0

以下是您開始使用的東西。

的標記本身是不是方便刮,但我們可以依靠和fixture行從夾具區分夾具和使用find_next_siblings() method從表中獲取的賠率排。

完整工作示例(調整它滿足您的需要):從BS4進口BeautifulSoup

R = requests.get( 「http://www.elitebetkenya.com/coupon.php?d」) 湯= BeautifulSoup(R

import requests 
from bs4 import BeautifulSoup 

r = requests.get("http://www.elitebetkenya.com/coupon.php") 
soup = BeautifulSoup(r.content, "html.parser") 


for league in soup.find_all("tr", class_="league"): 
    # get basic league and fixture data 
    league_name = league.get_text(strip=True) 

    fixture = league.find_next_sibling("tr", class_="fixture") 
    home = fixture.find("span", class_="home").get_text(strip=True) 
    away = fixture.find("span", class_="away").get_text(strip=True) 

    date = fixture.br.find_next_sibling(text=True).strip() 

    print(league_name, home, away, date) 

    # iterate over odds rows 
    for odd in fixture.find_next_siblings("tr"): 
     # stop once the next league is met 
     if "league" in odd.get("class", []): 
      break 

     # skipping header rows 
     if not odd.td.get_text(strip=True): 
      continue 

     cells = [td.get_text(strip=True) for td in odd.find_all("td")] 
     print(cells) 
+0

感謝alot..Had重新閱讀美麗的湯文檔來了解您的code..Second一天後,我註冊了這裏,並不能感到更受歡迎...順便說一句,看看我修改你的代碼的方式.. –

0

導入請求 。在soup.findAll( 「TR」)的含量)

實況= soup.prettify()

對於i: 個TDS = i.findAll(「TD」)

try: 
    home = i.find("span", {"class" :"home"}).get_text(strip=True) 
    away = i.find("span", class_="away").get_text(strip=True) 
    string = tds[0].text 
    g= [home,away,"vs"] 
    for i in g: 
     string.replace(i, "") 


    print string 


    # print "%s vs %s" %(home,away) 
except: 
    pass 
try: 


    if len(tds[0].text) != 0: 



     print " Type: %s Choice: %s, Match code: %s, 1: %s, 0: %s 2: %s" % \ 
      (tds[0].text,tds[1].text, tds[2].text,tds[3].text, tds[4].text, tds[5].text) 

except: 
    pass