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 ...
 
随机推荐
- js的Set和Map集合
			
目录 1.js的Set介绍 1-1.Set基础用法 1-2.Set对象的操作方法 1-3.Set对象的遍历方法 2.js的Set扩展WeakSet篇 3.js的Map介绍 3-1.Map基础用法 3- ...
 - 浅谈Spring框架
			
一.Spring简介 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构, 分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集 ...
 - 内网渗透之信息收集-Linux系统篇
			
linux 系统信息 grep MenTotal /proc/meminfo #查看系统内存总量 cat /etc/issue #查看系统名称 cat /etc/lsb-release #查看系统名称 ...
 - MySQL 【常识与进阶】
			
MySQL 事物 InnoDB事务原理 事务(Transaction)是数据库区别于文件系统的重要特性之一,事务会把数据库从一种一致性状态转换为另一种一致性状态. 在数据库提交时,可以确保要么所有修改 ...
 - python 异步MySQL存库
			
对于异步框架而言,这些延迟是无法接受的.因此, Twisted 提供了 twisted.enterprise.adbapi, 遵循DB-API 2.0协议的一个异步封装. adbapi 在单独的线程里 ...
 - Android平台接入OneNET
			
1. OneNET简介 中国移动物联网开放平台是由中国移动打造的PaaS物联网开放平台. 平台能够帮助开发者轻松实现设备接入与设备连接,提供综合性的物联网解决方案,实现物联网设备的数据获取,数据存储, ...
 - ILspy不能查看VS代码问题
			
1.问题情况 刚接触IL,发现从网上下载的IL无法反编译 dll和exe文件都不行 出现如下提示: System.OutOfMemoryException: 数组维度超过了支持的范围. 2.解决方法 ...
 - Natas20 Writeup(Session登录,注入参数)
			
Natas20: 读取源码,发现把sessionID存到了文件中,按键值对存在,以空格分隔,如果$_SESSION["admin"]==1,则成功登陆,得到flag.并且通过查询所 ...
 - Web_jQuery
			
第1章: jQuery简介 为了简化 JavaScript 的开发,一些 JavsScript 库诞生了. JavaScript库封装了很多预定义的对象和实用函数,简化HTML与JavaScript之 ...
 - [Dynamic Programming]动态规划之背包问题
			
动态规划之背包问题 例题 现有4样物品n = ['a', 'b', 'c', 'd'],重量分别为w = [2, 4, 5, 3],价值分别为v = [5, 4, 6, 2].背包最大承重c = 9. ...