相关疑难解决方法(0)

TypeError:需要类似字节的对象,而不是python和CSV中的'str'

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)
Run Code Online (Sandbox Code Playgroud)

在最后一行上方.

csv html-table beautifulsoup python-3.x

141
推荐指数
4
解决办法
16万
查看次数

标签 统计

beautifulsoup ×1

csv ×1

html-table ×1

python-3.x ×1