使用python爬虫,用beautifulsoup解析网页的时候,网页的中文乱码。
查看一下当前的encoding:
print(title.encoding)
发现网页编码不是utf8
尝试使得:
title.encoding = title.apparent_encoding
乱码解决!
完整代码:
title = requests.get(title_url,headers = headers)
print(title.encoding)
title.encoding = title.apparent_encoding
title = BeautifulSoup(title.text,"lxml")
title_word = title.find_all('h1')
keyword = title_word[0].string