为什么python3 使用list.append(元素)。元素前后类型变了?

为什么python3 使用list.append(元素)。元素前后类型变了?

        print(type(type1[0].string))

ulist2.append([type1[0].sting, type1[1].sting, type1[2].sting, type1[3].sting])

print(type(type1[0].sting))

输出:

<class 'bs4.element.NavigableString'>
<class 'NoneType'>

完整代码:

import requests

from bs4 import BeautifulSoup

import bs4

def get_html(url):

try:

r = requests.get(url)

r.raise_for_status()

r.encoding = r.apparent_encoding

return r.text

except Exception as f:

print("fault:{}".format(f))

return ''

def parser_html(ulist, html):

soup = BeautifulSoup(html,'html.parser')

n = 0

for tag in soup.tbody.children:

n += 1

ulist2 = []

if isinstance(tag, bs4.element.Tag):

# print(tag)

# print(type(tag))

type1 = tag.find_all('td')

print(type(type1))

print(type1)

print(type1[0].string)

print(type1[1].string)

print(type(type1[0].string))

# for tag2 in type1:

#

# ulist2.append(tag2.string)

ulist2.append([type1[0].sting, type1[1].sting, type1[2].sting, type1[3].sting])

print(type(type1[0].sting))

# ulist2 = ulist2[:4]

print(ulist2)

ulist = ulist + ulist2

print(ulist)

if n > 10:

return ulist

return ulist

# ulist.append([tag[0].string, tag[1].string, tag[2].string])

def print_info(ulist, num):

print(ulist)

for i in range(num):

print('{0:<10}\t{1:{4}<25}\t{2:<10}\t{3:<10}'.format(ulist[i][0],ulist[i][1], ulist[i][2], ulist[i][3], chr(12288)))

def main():

ulist = []

url = "http://www.zuihaodaxue.com/Greater_China_Ranking2019_0.html"

html = get_html(url)

parser_html(ulist, html)

print_info(ulist, 5)

if __name__ == "__main__":

main()


回答:

代码单词敲错了。

print(type(type1[0].sting)) ----> print(type(type1[0].string))


回答:

sting还行...我倒是想问为啥没报错

以上是 为什么python3 使用list.append(元素)。元素前后类型变了? 的全部内容, 来源链接: utcz.com/a/161982.html

回到顶部