python3出现TypeError: can only concatenate str (not "bytes") to str的错误,大一刚上手python的学习,望大神解决。感激不尽

 print(city.string + u'\nAQI指数:' + aqi.string+ u'\nPM2.5浓度:' + pm25.string + pm25danwei.string + u'\n空气质量:' + quality.string + space.sub("",replacechar.sub('',str(result))).encode('utf-8'))
TypeError: can only concatenate str (not "bytes") to str

==========完整代码如下:

import urllib.request
import threading
from time import ctime
import bs4    #besutifulsoup的第三版
import re

def getPM25(cityname):
    site = 'http://www.pm25.com/city/' + cityname + '.html'
    html = urllib.request.urlopen(site)
    soup =bs4.BeautifulSoup(html)

    city = soup.find("span",{"class":"city_name"})  # 城市名称
    aqi = soup.find("a",{"class":"cbol_aqi_num"})   # AQI指数
    pm25 = soup.find("span",{"class":"cbol_nongdu_num_1"})   # pm25指数
    pm25danwei = soup.find("span",{"class":"cbol_nongdu_num_2"})   # pm25指数单位
    quality = soup.find("span",{"class":re.compile('cbor_gauge_level\d$')})  # 空气质量等级
    result = soup.find("div",{"class":'cbor_tips'})   # 空气质量描述
    replacechar = re.compile("<.*?>")  #为了将<>全部替换成空
    space = re.compile(" ")
    print(city.string + u'\nAQI指数:' + aqi.string+ u'\nPM2.5浓度:' + pm25.string + pm25danwei.string + u'\n空气质量:' + quality.string + space.sub("",replacechar.sub('',str(result))).encode('utf-8'))
    print('*'*20 + ctime() + '*'*20)

def one_thread(cityname1):   # 单线程
    print('One_thread Start: ' + ctime() + '\n')
    getPM25(cityname1)

def two_thread():   # 多线程
    print('Two_thread Start: ' + ctime() + '\n')
    threads = []
    t1 = threading.Thread(target=getPM25,args=('beijing',))
    threads.append(t1)
    t2 = threading.Thread(target=getPM25,args=('shenyang',))
    threads.append(t2)

    for t in threads:
        # t.setDaemon(True)
        t.start()


if __name__ == '__main__':

    print("*"*20+"welcome to 京东放养的爬虫"+"*"*20)
    while True:
        cityname1 = input("请输入想要查看的城市名称:(例如:beijing)")
        if cityname1 == 'quit':
            break
        one_thread(cityname1)
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值