TypeError:需要类似字节的对象,而不是'str'
在执行下面的python代码时将错误的表格数据保存在Csv文件中 . 不知道如何获得rideup.pls帮助我 .
import csv
import requests
from bs4 import BeautifulSoup
url='http://www.mapsofindia.com/districts-india/'
response=requests.get(url)
html=response.content
soup=BeautifulSoup(html,'html.parser')
table=soup.find('table', attrs={'class':'tableizer-table'})
list_of_rows=[]
for row in table.findAll('tr')[1:]:
list_of_cells=[]
for cell in row.findAll('td'):
list_of_cells.append(cell.text)
list_of_rows.append(list_of_cells)
outfile=open('./immates.csv','wb')
writer=csv.writer(outfile)
writer.writerow(["SNo", "States", "Dist", "Population"])
writer.writerows(list_of_rows)
在最后一行上方 .
3 回答
您使用的是Python 2方法而不是Python 3 .
更改:
至:
并且您将获得具有以下输出的文件:
在Python 3中,csv以文本模式获取输入,而在Python 2中,它以二进制模式获取 .
Edited to Add
这是我运行的代码:
我对Python3也有同样的问题 . 我的代码写入
io.BytesIO()
.替换为
io.StringIO()
已解决 .在我的例子中,我使用BeautifulSoup用Python 3.x编写.txt . 它有同样的问题 . 正如@tsduteba所说,将第一行的'wb'改为'w' .