TypeError:需要一個(gè)類(lèi)似字節(jié)的對(duì)象,而不是'str'在執(zhí)行以下python代碼以將HTML表格數(shù)據(jù)保存到Csv文件時(shí)遇到上述錯(cuò)誤。不知道如何獲得rideup.pls幫助我。import csvimport requestsfrom bs4 import BeautifulSoupurl='http://www.mapsofindia.com/districts-india/'response=requests.get(url)html=response.contentsoup=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 回答

守著星空守著你
TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊
file = open('parsed_data.txt', 'w')
for link in soup.findAll('a', attrs={'href': re.compile("^http")}): print (link)
soup_link = str(link)
print (soup_link)
file.write(soup_link)
file.flush()
file.close()
就我而言,我使用BeautifulSoup用Python 3.x編寫(xiě)了一個(gè).txt。它有同樣的問(wèn)題。就像@tsduteba所說(shuō)的那樣,將第一行中的'wb'更改為'w'。

料青山看我應(yīng)如是
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
我在Python3中遇到了同樣的問(wèn)題。我的代碼正在寫(xiě)入io.BytesIO()
。
替換為已io.StringIO()
解決。
添加回答
舉報(bào)
0/150
提交
取消