网络爬虫,为什么会报错了

不懂是哪里错了

img

碰到了none 值
可以在 write 前,加个判断 u 是否为None

图中有很多问题,代码写的太乱了看的眼花,我指出比较明了的问题吧
1.html编码有问题,此网页编码为gb2312,而不是utf-8
2.beautiful节点选的有问题,导致节点返回None

整体排行榜我写了一下,仅供参考,csv部分自己补充吧

 
import datetime
import time
from lxml import etree
import requests
import logging
BASE_URL='https://www.igo.cn/zt/University_Rankings/'
def scrape_url(url):
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
    print(datetime.date.today(),time.strftime("%H:%M:%S"),'正在爬取网站为:{} ...'.format(url))
    try:
        response=requests.get(url,headers=headers)
        response.encoding='gb2312'
        if response.status_code==200:
            return response.text
    except requests.RequestException:
        logging.error('error occurred while scraping %s',url,exc_info=True)
def parser_html():
    html=scrape_url(BASE_URL)
    dom=etree.HTML(html)
    xp=dom.xpath('//tr/td[1]/text()')
    xp_1=dom.xpath('//tr/td[2]/text()')
    xp_2=dom.xpath('//tr/td[3]/text()')
    xp_3=dom.xpath('//tr/td[4]/text()')
    xp_4=dom.xpath('//tr/td[5]/text()')
    for i in range(0,201):
        print('排名:'+str(xp[i].replace('/td>',''))+'\t',
              '中文名:'+xp_1[i]+'\t',
              '英文名:'+xp_2[i]+'\t',
              '国家/地区:'+xp_3[i]+'\t',
              '得分:'+str(xp_4[i]))
if __name__ == '__main__':
    parser_html()