今日老同学来问我有关Python爬虫urllib的一些用法,恰逢错误一道。于是以此做个记录…

运行环境 Runtime environment

1
2
3
4
操作系统 : Windows10  
IDE: JetBrains Pycharm 2018.2.4 x64
浏览器: Google Chrome 版本 67.0.3396.99(正式版本) (64 位)&& FireFox Developer Edition 版本63.0b4 (64位)
Python: 3.6.2

症状

尝试了来自老同学发来的urllib爬虫代码,目的是为了通过使用python3.x执行post请求时报错

报错信息:”POST data should be bytes or an iterable of bytes. It cannot be of type str.”

解决办法

发现需要在urlencode语句后加encode(encoding=’UTF8’)

params = urllib.parse.urlencode(data)

将其修改为:

params = urllib.parse.urlencode(data).encode(encoding=’UTF8’)

想详细了解更多有关Github贡献计算的规则可以到官网了解更多:

https://help.github.com/articles/why-are-my-contributions-not-showing-up-on-my-profile/

即可解决更多问题!