小白python爬虫学习1(vscode+乱码)

import requests
from bs4 import BeautifulSoup
import chardet
 

target_url = "https://www.dmzj.com/info/yaoshenji.html"
r = requests.get(url=target_url)


bs = BeautifulSoup(r.text, 'lxml')
list_con_li = bs.find('ul', class_="list_con_li")
comic_list = list_con_li.find_all('a')
chapter_names = []
chapter_urls = []
for comic in comic_list:
    href = comic.get('href')
    name = comic.text
    chapter_names.insert(0, name)
    chapter_urls.insert(0, href)

print(chapter_names)
print(chapter_urls)

准备从一个无名小说网站获取目录和链接,然鹅在vscode打印出来发现中文乱码

查阅其他博客,发现可能的问题

1.是网页编码虽然是utf-8,但还用了gzip怀疑可能影响,之后找解决方法,但还没找到可以用的

2vscode本身的问题,网上查了关于print的问题,找到了俩个简便的方法

1不用run code,用调试即f5来解决问题,乱码问题不在,

2在代码前填上

 
import io
import sys
#改变标准输出的默认编码
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

也可以解决,

至于为啥是这么个结果我也不懂,希望有大佬帮忙解释一下

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值