I have tried this two different ways and cannot get either to work. I am trying to scrape the stats on this webpage: http://www.cbssports.com/nfl/gametracker/boxscore/NFL_20161120_JAC@DET/ within "TEAM STATS". I need the numbers after a specified stat category ex: "NET YARDS RUSHING". Below is what I have tried without any success.
First way:
import pickle
import math
import os
import urllib2
from lxml import etree
from bs4 import BeautifulSoup
from urllib import urlopen
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles import colors
from openpyxl.cell import Cell
Last Two Game info Home [H] or Away [A]
favLastGM = 'H' #Higher week number 2
favLastGM2 = 'A' #Lower week number 1
#Game Info (Favorite) Last Game Played - CBS Sports (Change Every Week)
favPrevGMInfoUrl = 'http://www.cbssports.com/nfl/gametracker/boxscore/NFL_20161120_JAC@DET/'
response8 = urllib2.urlopen(favPrevGMInfoUrl)
htmlparser8 = etree.HTMLParser()
tree8 = etree.parse(response8,htmlparser8)
#FAVORITE
if favLastGM == 'A': #This Gives Opposite of Away Team Net Rushing Yards - SO HOME Net Rushing Yards
text = tree8.xpath('//td[contains(text(),"Net Yards Rushing")]/parent::td/following-sibling::td[1]/text()')
if text:
favDef_rushYards_L2_1 = int(text[0].strip())
print("test"),
print favDef_rushYards_L2_1
print ("Enter: Total Rushing Yards Allowed from Favored Team Defense for last game played: "),
print favDef_rushYards_L2_1
elif favLastGM == 'H': #This Gives Opposite of Home Team Net Rushing Yards - SO AWAY Net Rushing Yards
text = tree8.xpath('//td[contains(text(),"Net Yards Rushing")]/parent::td/following-sibling::td[0]/text()')
if text:
favDef_rushYards_L2_1 = int(text[0].strip())
print("test"),
print favDef_rushYards_L2_1
print ("Enter: Total Rushing Yards Allowed from Favored Team Defense for last game played: "),
print favDef_rushYards_L2_1
else:
print("***************************************************")
print("NOT A VALID ENTRY - favLastGM !")
print("***************************************************")
Second way:
import pickle
import math
import os
import urllib2
from lxml import etree
from bs4 import BeautifulSoup
from urllib import urlopen
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles import colors
from openpyxl.cell import Cell
#Last Two Game info Home [H] or Away [A]
favLastGM = 'H' #Higher week number 2
favLastGM2 = 'A' #Lower week number 1
#Game Info (Favorite) Last Game Played - CBS Sports (Change Every Week)
favPrevGMInfoUrl = 'http://www.cbssports.com/nfl/gametracker/boxscore/NFL_20161120_JAC@DET/'
favPrevGMhtml2 = urlopen(favPrevGMInfoUrl).read()
favPrevGMsoup2 = BeautifulSoup(favPrevGMhtml2)
favPrevGM2Reg = favPrevGMsoup2.find("table", { "class" : "team-stats" })
favPrevGM2Reg2 = []
if favLastGM == 'A': #This Gives Opposite of Away Team Net Rushing Yards - SO HOME Net Rushing Yards
rush = 'Net Yards Rushing'
for row in favPrevGM2Reg.findAll("tr"):
if rush in row.findNext('td'): #Change Year for every new season
for item in row.findAll("td"):
favPrevGM2Reg.append(item.text)
favDef_rushYards_L2_1 = float(favPrevGM2Reg[1])
print ("Enter: Total Rushing Yards Allowed from Favored Team Defense for last game played: "),
print favDef_rushYards_L2_1
elif favLastGM == 'H': #This Gives Opposite of Home Team Net Rushing Yards - SO AWAY Net Rushing Yards
rush = 'Net Yards Rushing'
for row in favPrevGM2Reg.findAll("tr"):
if rush in row.findNext('td'): #Change Year for every new season
for item in row.findAll("td"):
favPrevGM2Reg.append(item.text)
favDef_rushYards_L2_1 = float(favPrevGM2Reg[0])
print ("Enter: Total Rushing Yards Allowed from Favored Team Defense for last game played: "),
print favDef_rushYards_L2_1
else:
print("***************************************************")
print("NOT A VALID ENTRY - favLastGM !")
print("***************************************************")
You are looking for the xpath:
//td[contains(text(),"Net Yards Rushing")]/following-sibling::td
What this does is select the beginning td, which you did right, but you want its siblings, not its parents' siblings, so you need to add following-sibling::td directly after the td. This will give you 2 results in order of appearance in the table
I didn't write out the whole code, but these two lines will give you the home and away rushing yards.
import urllib2
from lxml import etree
favPrevGMInfoUrl = 'http://www.cbssports.com/nfl/gametracker/boxscore/NFL_20161120_JAC@DET/'
response8 = urllib2.urlopen(favPrevGMInfoUrl)
htmlparser8 = etree.HTMLParser()
tree8 = etree.parse(response8,htmlparser8)
away = tree8.xpath('//tr[@data-category="rushing_yards"]//td[@class="stat-value away"]/text()')
home = tree8.xpath('//tr[@data-category="rushing_yards"]//td[@class="stat-value home"]/text()')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.