Python爬取中国最好大学排行榜报错TypeError: unsupported format string passed to NoneType.__format__

简介: Python爬取中国最好大学排行榜报错TypeError: unsupported format string passed to NoneType.__format__


本文使用的是如下网址:

http://gaokao.xdf.cn/201911/10991728.html

1 问题分析与解决

报错为类型错误,显示我们传递了不支持的格式字符串

1.1 strip()

请添加链接描述
我们查看网页源码,发现我们所传递的字符串头尾包含空格及换行(红色方框),但是这不是报错的原因,这只会导致格式不太好看,因此我在获取字符串是添加了.strip()函数,既tds[0].text.strip()。
strip()函数可去除头尾的指定字符,默认为空格及换行。

1.2 string与text

r.text                           #响应内容的字符串形式,即url对应页面的内容
r.string                        #标签内非属性字符串,<>...<>中字符串,格式:.string

通过对比我们可以发现r.string获取的是标签内非属性字符串,而我们查看源代码可以发现大学名字不是td标签的字符串,属于td儿子的儿子的儿子……的字符串,因此tds[0].string只能获取到None。

所以应该使用r.text获取,即tds[1].text.strip()

 ulist.append([tds[0].text.strip(), tds[1].text.strip(), tds[3].text.strip()])       

2 爬取结果

排名    .   学校名称       .    总分    
1         .   清华大学       .   北京市    
2         .   北京大学       .   北京市    
3         .   浙江大学       .   浙江省    
4         .  上海交通大学      .   上海市    
5         .   复旦大学       .   上海市    
6         .   南京大学       .   江苏省    
7         . 中国科学技术大学     .   安徽省    
8         . 哈尔滨工业大学      .   黑龙江省   
9         .  华中科技大学      .   湖北省    
10        .   中山大学       .   广东省    
11        .   东南大学       .   江苏省    
12        .   天津大学       .   天津市    
13        .   同济大学       .   上海市    
14        . 北京航空航天大学     .   北京市    
15        .   四川大学       .   四川省    
16        .   武汉大学       .   湖北省    
17        .  西安交通大学      .   陕西省    
18        .   南开大学       .   天津市    
19        .  大连理工大学      .   辽宁省    
20        .   山东大学       .   山东省   

3 源码显示

import requests
from bs4 import BeautifulSoup
import bs4

#获取url内容
def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""

#数据提取填充
def fillUnivList(ulist, html):
    soup = BeautifulSoup(html, "html.parser")
    for tr in soup.find('tbody').children:
        if isinstance(tr, bs4.element.Tag):
            tds = tr('td')
            ulist.append([tds[0].text.strip(), tds[1].text.strip(), tds[3].text.strip()])       #.strip()去除头尾空格、换行

#格式化输出
def printUnivList(ulist, num):
    tplt = "{0:^10}\t{1:{3}^10}\t{2:^10}"
    print(tplt.format("排名", "学校名称", "总分", chr(12288)))
    for i in range(num):
        u = ulist[i]
        print(tplt.format(u[0],u[1],u[2],chr(12288)))

#主函数
def main():
    uinfo = []
    url = 'http://gaokao.xdf.cn/201911/10991728.html'
    html = getHTMLText(url)
    fillUnivList(uinfo,html)
    printUnivList(uinfo,20)  # 20 univs


main()

相关文章
|
9天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
189 100
|
9天前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
206 99
|
12天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
12天前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
2月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
212 92
|
3月前
|
数据采集 机器学习/深度学习 边缘计算
Python爬虫动态IP代理报错全解析:从问题定位到实战优化
本文详解爬虫代理设置常见报错场景及解决方案,涵盖IP失效、403封禁、性能瓶颈等问题,提供动态IP代理的12种核心处理方案及完整代码实现,助力提升爬虫系统稳定性。
190 0
|
Ubuntu Python
【Python】报错ModuleNotFoundError: No module named ‘XXX‘
【Python】报错ModuleNotFoundError: No module named ‘XXX‘
|
12月前
|
Linux Python
【Azure Function】Python Function部署到Azure后报错No module named '_cffi_backend'
ERROR: Error: No module named '_cffi_backend', Cannot find module. Please check the requirements.txt file for the missing module.
214 2
|
Ubuntu Python
Python(六)使用pycharm创建项目报错:ModuleNotFoundError No module named distutils.util
在ubuntu上使用pycharm创建项目的时候报错: ModuleNotFoundError: No module named 'distutils.util'
1678 0

热门文章

最新文章

推荐镜像

更多