Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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 为什么美丽的汤不能显示全部<;td>;表格中的数据?_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 为什么美丽的汤不能显示全部<;td>;表格中的数据?

Python 为什么美丽的汤不能显示全部<;td>;表格中的数据?,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,一周前我试着翻阅维基百科。但我不明白为什么Beautiful Soup只显示表列中的一些字符串,而对其他表列显示“none” 注意:表列中全部包含数据 我的程序将提取标记为“description”的所有表列。我试图从表中提取所有描述 我正在抓取的网站是:) 这是我的代码: from BeautifulSoup import BeautifulSoup import urllib import sys from urllib import FancyURLopener class MyOpe

一周前我试着翻阅维基百科。但我不明白为什么Beautiful Soup只显示表列中的一些字符串,而对其他表列显示“none”

注意:表列中全部包含数据

我的程序将提取标记为“description”的所有表列。我试图从表中提取所有描述

我正在抓取的网站是:)

这是我的代码:

from BeautifulSoup import BeautifulSoup 
import urllib
import sys
from urllib import FancyURLopener

class MyOpener(FancyURLopener):
    version = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.65 Safari/534.24'


def printList(rowList):
    for row in rowList:
        print row
        print '\n'

    return

url = "http://en.wikipedia.org/wiki/Supernatural_(season_6)"

#f = urllib.urlopen(url)
#content = f.read()
#f.close

myopener = MyOpener()
page = myopener.open(url)
content = page.read()
page.close()

soup = BeautifulSoup(''.join(content))
soup.prettify()

movieList = []

rowListTitle = soup.findAll('tr', 'vevent')
print len(rowListTitle)

#printList(rowListTitle)
for row in rowListTitle:
    col = row.next # explain this?
    if col != 'None':
        col = col.findNext("b")
        movieTitle = col.string
        movieTuple = (movieTitle,'')
        movieList.append(movieTuple)

#printList(movieList)

for row in movieList:
    print row[0]

rowListDescription = soup.findAll('td' , 'description')
print len(rowListDescription)


index = 1;
while ( index < len(rowListDescription) ):
    description = rowListDescription[index]
    print description
    print description.string
    str = description
    print '####################################'
    movieList[index - 1] = (movieList[index - 1][0],description)
    index = index + 1
从美化组导入美化组
导入URL库
导入系统
从urllib导入FancyURLopener
类开孔器(开孔器):
版本='Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/534.24(KHTML,类似Gecko)Chrome/11.0.696.65 Safari/534.24'
def打印列表(行列表):
对于行列表中的行:
打印行
打印“\n”
返回
url=”http://en.wikipedia.org/wiki/Supernatural_(第六季)
#f=urllib.urlopen(url)
#content=f.read()
#f、 接近
myopener=myopener()
page=myopener.open(url)
content=page.read()
page.close()
汤=BeautifulSoup(“”.join(内容))
汤
电影列表=[]
rowListTitle=soup.findAll('tr','vevent')
打印长度(行列表标题)
#打印列表(rowListTitle)
对于rowListTitle中的行:
col=row.next#解释一下?
如果col!='“没有”:
col=col.findNext(“b”)
movieTitle=col.string
电影片段=(电影片段“”)
movieList.append(movieTuple)
#打印列表(电影列表)
对于movieList中的行:
打印行[0]
rowListDescription=soup.findAll('td','description')
打印长度(行列表描述)
指数=1;
而(索引

我没有粘贴输出,因为它确实很长。但是输出非常奇怪,因为它成功地捕获了
中的信息,但是当我执行
.string
时,它会给我一个空内容

所有描述字符串都是空的吗?从文件中:

为方便起见,如果标记只有一个子节点,且该子节点是字符串,则该子节点将作为tag.string和tag.contents[0]提供


在这种情况下,描述通常有子节点,即:指向另一篇Wikipedia文章的
链接。这算作非字符串子节点,在这种情况下,描述节点的
string
设置为
None

。tag.contents[0]确实有助于检索第一个NavigableString。