python 报错:a bytes-like object is required, not 'str'
核心代码:
def ipPools(numPage):
headers = randomHeads()
url = 'http://www.xicidaili.com/nn/'
saveFsvFile = open('ips.csv', 'wb')
writer = csv.writer(saveFsvFile)
for num in range(1, numPage + 1):
full_url = url + str(num)
re = requests.get(full_url, headers=headers)
soup = BeautifulSoup(re.text, 'lxml')
res = soup.find(id="ip_list").find_all('tr')
for item in res:
try:
temp = []
tds = item.find_all('td')
proxyIp = tds[1].text.encode("utf-8")
proxyPort = tds[2].text.encode("utf-8")
temp.append(proxyIp)
temp.append(proxyPort)
writer.writerow(temp)
print('保存为excel成功!')
except IndexError:
pass
注意点:
一定要将str转为bytes :
str.encode("utf-8")
python36 file方法改为open
open('ips.csv', 'wb')将wb改为w 我出错就在这。 如果有相同错误可以,作为参考吧! 推荐链接:
https://stackoverflow.com/questions/43582925/python-a-bytes-like-object-is-required-not-str-while-printing
https://blog.csdn.net/csu_vc/article/details/78372932 这两个可以看下。
python 报错:a bytes-like object is required, not 'str'的更多相关文章
- sbt package报错:a bytes-like object is required, not 'str'
Traceback (most recent call last): File , in <module> s.sendall(content) TypeError: a bytes-li ...
- moviepy执行TextClip.search方法时报错TypeError: a bytes-like object is required, not str
☞ ░ 前往老猿Python博文目录 ░ 执行TextClip.search方法时,报错: >>> from moviepy.editor import * >>> ...
- python问题:TypeError: a bytes-like object is required, not 'str'
源程序: import socket target_host = "www.baidu.com" # 127.0.0.1 target_port = 80 # 建立一个socket ...
- python报错:TypeError: 'int' object is not subscriptable
检查一遍报错的所在行,此报错一般是在整数上加了下标: 比如: a = 4 c=a[2] 报错:line 2, in <module> c=a[2] TypeError: 'i ...
- python 3.5: TypeError: a bytes-like object is required, not 'str'
出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了s ...
- 安装pandas报错(AttributeError: 'module' object has no attribute 'main')
在pycharm中安装pandas出现报错:AttributeError: 'module' object has no attribute 'main', 刚开始以为是pip的版本太旧了,于是乎将其 ...
- 解决:pipenv shell报错:AttributeError: 'module' object has no attribute 'run'
利用pipenv shell切换到虚拟环境时,显示报错:AttributeError: 'module' object has no attribute 'run' 可以看到是d:\program\p ...
- Python报错:UnicodeEncodeError 'gbk' codec can't encode character
今天在使用Python文件处理写网络上爬取的文件的时候,遇到了错误:UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa0’ in p ...
- Python报错总结丶自定义报错
Python报错总结: 常见异常 1,NameError: name 'a' is not defined:未定义函数名 2,IndentationError: uninden ...
随机推荐
- oracle的wm_concat函数实现行转列
有以下数据 deptname phone username isboss 部门A 电话1 员工A 0 部门A 电话1 领导A 1 部门B 电话2 员工B 0 部门B 电话2 员工C 0 部门B 电话2 ...
- 什么是SSH与SSH客户端
1.什么是SSH? SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SS ...
- 关于OSS不再维护的一些讨论
FUSE for macOS 将不再维护 Fuse 是一款针对Mac OS的文件系统所开发的一款开源软件. 用于MacOS的FUSE软件包提供了多个API,用于为OS X 10.9至macOS 10. ...
- [Python] iupdatable包:日志模块使用介绍
一.说明 日志模块是对 logging 模块的单例封装 特点: 可同时向控制台和文件输出日志,并可选择关闭其中一种方式的输出: 集成colorlog,实现根据日志等级不同,控制台输出日志颜色不同: 灵 ...
- MATLAB神经网络(6) PID神经元网络解耦控制算法——多变量系统控制
6.1 案例背景 6.1.1 PID神经元网络结构 PID神经元网络从结构上可以分为输入层.隐含层和输出层三层,$n$个控制量的PID神经元网络包含$n$个并列的相同子网络,各个子网络间既相互独立,又 ...
- openstack的yum源出错,配置openstack-ocata版的在线yum源,openstack的yum源配置
真的是几经周折,终于配置好了!我做好了一键配置yum的代码,地址:https://www.cnblogs.com/guarding/p/12321702.html 首先看一下配置前的报错信息把: 需要 ...
- h5微信中视频禁止全屏
<video id="videoPlayer" v-show="isShowVideo" class="video" ref=&quo ...
- Python基础 | 关于“循环”那些事
目录 for 循环 list range enumerate zip while 循环 while相当于if时 while充当for和if的混合体 泛循环 列表解析 map 迭代器 生成器 循环的跳出 ...
- Java基础 - 原码、反码、补码
目录 机器数 真值 原码 反码 补码 为什么使用原码. 反码. 补码 机器数 所有数字在计算机底层都是以二进制形式存在的.它的表现形式叫做机器数,这个数有正负之分,最高位为符号位.0 表示正数, 1 ...
- Linux中更换为国内镜像源
推荐使用清华镜像:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ 将下列文本添加到/etc/apt/sources.list文件里 # 默认注释了源 ...