Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我不能用Python解析这个HTML页面?_Python_Html_Beautifulsoup - Fatal编程技术网

为什么我不能用Python解析这个HTML页面?

为什么我不能用Python解析这个HTML页面?,python,html,beautifulsoup,Python,Html,Beautifulsoup,我正在尝试使用BeautifulSoup用Python解析网页中的信息。我想打印出桌上团队投手的每个球员的相应姓名”。但是,代码在某个特定名称之后重复玩家的名称,在本例中,在第15行之后,它重复了名称“Pedro Martinez”。例如: 1 Pedro Martinez 2 Jeff Fassero* 3 Ramon Martinez 4 Pete Schourek* 5 Rolando Arrojo 6 Tomo Ohka 7 Derek Lowe 8 Tim Wakefield 9 Ri

我正在尝试使用BeautifulSoup用Python解析网页中的信息。我想打印出桌上团队投手的每个球员的相应姓名”。但是,代码在某个特定名称之后重复玩家的名称,在本例中,在第15行之后,它重复了名称“Pedro Martinez”。例如:

1 Pedro Martinez
2 Jeff Fassero*
3 Ramon Martinez
4 Pete Schourek*
5 Rolando Arrojo
6 Tomo Ohka
7 Derek Lowe
8 Tim Wakefield
9 Rich Garces
10 Rheal Cormier*
11 Hipolito Pichardo
12 Brian Rose
13 Bryce Florie
14 John Wasdin
15 Pedro Martinez
16 Jeff Fassero*
17 Ramon Martinez
18 Pete Schourek*
19 Rolando Arrojo
20 Tomo Ohka
21 Derek Lowe
22 Tim Wakefield
23 Rich Garces
24 Rheal Cormier*
25 Hipolito Pichardo
26 Brian Rose
27 Bryce Florie
28 John Wasdin
你知道发生了什么事吗?这是我的代码:

# Sample web page
#http://www.baseball-reference.com/teams/BOS/2000-pitching.shtml


import urllib2
import lxml
from bs4 import BeautifulSoup

# Download webpages 2010 webpage

y = 2000
url = 'http://www.baseball-reference.com/teams/BOS/'+ str(y) +'-pitching.shtml'
print 'Download from :', url

#dowlnload
filehandle = urllib2.urlopen(url)


fileout = 'YEARS'+str(y)+'.html'
print 'Save to : ', fileout, '\n'

#save file to disk
f = open(fileout,'w')
f.write(filehandle.read())
f.close()


# Read and parse the html file

# Parse information about the age of players in 2000

y = 2000

filein = 'YEARS' + str(y) + '.html'
print(filein)
soup = BeautifulSoup(open(filein))


entries = soup.find_all('tr', attrs={'class' : ''}) #' non_qual' ''
print(len(entries)) 

i = 0
for entry in entries:


    columns = entry.find_all('td')  
    #print(len(columns), 'i:', i)
    if len (columns)==34: # Number of columns of the table

        i = i + 1

        #print i, len(columns)  
        age = columns[2].get_text()

        print i, age

您试图循环遍历表中的所有行,但实际上没有首先获得所有表标记。所以你得到所有的表标记,然后循环遍历表标记中的所有tr标记,如果这有意义的话。年份和表格也没有定义,所以我假设年份是y,并将表格变量t设为。作为补充说明,您不必下载HTML然后打开它来解析它,您只需获取连接文本并直接解析即可获得HTML

import urllib2
from bs4 import BeautifulSoup

# Download webpages 2010 webpage

y = 2010
url = 'http://www.baseball-reference.com/teams/BOS/'+ str(y) +'-pitching.shtml'
print 'Download from :', url

#dowlnload
filehandle = urllib2.urlopen(url)


fileout = 'YEARS'+str(y)+'.html'
print 'Save to : ', fileout, '\n'

#save file to disk
f = open(fileout,'w')
f.write(filehandle.read())
f.close()


# Read and parse the html file

# Parse information about the age of players in 2000

y = 2010

filein = 'YEARS' + str(y) + '.html'
print(filein)
soup = BeautifulSoup(open(filein))


table = soup.find_all('table', attrs={'id': 'team_pitching'}) #' non_qual' ''


for t in table:

    i = 1
    entries = t.find_all('tr', attrs={'class' : ''}) #' non_qual' ''
    print(len(entries))
    for entry in entries:
        columns = entry.find_all('td')
        printString = str(i) + ' '
        for col in columns:
            try:
                if ((',' in col['csk']) and (col['csk'] != '')):
                    printString = printString + col.text
                    i = i + 1
                    print printString
            except:
                pass

你想提取吗?表是什么?表在哪里?您显示的代码有两种方式-使用变量year和table,从未定义。将其修复为使用y和soup,它运行良好-不会重现您提到的错误,2010年也没有Pedro Martinez:它是John Lackey、Jon Lester等。使用文本中提到的2000,而不是代码中提到的2010,再次修复上述wrt变量名,1确实是Pedro Martinez,但同样,没有错误-15是Rod Beck,等等,海因斯特,你的建议很有道理。然而,程序仍然在重复第15行之后的名称,我的意思是它在Pedro Martinez中再次启动?有什么想法吗?我没有在桌子上看到佩德罗·马丁内斯@PaulI使用2000年的数据,而不是2010年的数据。但在第15排之后,我仍然看到了问题@heinst@Paul是的,我现在明白了,这个方法肯定有效!谢谢@heinst,它适用于2010年。你知道为什么这种方法不适用于2000年的数据吗?