我有一个程序可以从网站上打印信息,但是我想将此信息放入csv或excel文件中。这就是我所做的:
import requests
from bs4 import BeautifulSoup
import re
import xlsxwriter
workbook = xlsxwriter.Workbook('nossarede.xlsx')
worksheet = workbook.add_worksheet()
request = requests.get("http://www.drogariasnossarede.com.br/nossas-lojas")
soup = BeautifulSoup(request.content, 'html.parser')
data = soup.find_all("div", class_='item')
for container in data:
Pharmacyname = container.find_all("h3")
Pharmacyadd = container.find_all("p")
for pharmacy in Pharmacyname:
print(pharmacy.text)
for add in Pharmacyadd:
print(add.text)
print('')
直到这是完美的,它以我想要的方式完美打印。然后我尝试了这个:
import csv
with open('names.csv', 'wb') as ofile:
writer = csv.writer(ofile)
for container in data:
Pharmacyname = container.find_all("h3")
Pharmacyadd = container.find_all("p")
for pharmacy in Pharmacyname:
for add in Pharmacyadd:
writer.writerow((pharmacy.text[0], add.text[1]))
但是它不起作用。 它显示了这一点:
TypeError: a bytes-like object is required, not 'str'
我该怎么做? 谢谢!
答案 0 :(得分:2)
您正在使用wb
以字节模式打开文件。
只需更改此:
with open('names.csv', 'wb') as ofile:
对于
with open('names.csv', 'w') as ofile:
还有一件事。您的writerow
是错误的。您只会得到字符串的第一个字符。
更改此:
writer.writerow((pharmacy.text[0], add.text[0]))
针对:
writer.writerow((pharmacy.text, add.text))
答案 1 :(得分:1)
更改:
public Task<TResult> ProcessAsync<TQuery, TResult>(IQuery<TQuery, TResult> query)
where TQuery: IQuery<TQuery, TResult>
{
var handler = serviceProvider.Resolve<QueryHandler<TQuery, TResult>>();
// etc.
}
是写字节模式,用于:
with open('names.csv', 'wb') as ofile:
完整代码:
with open('names.csv', 'w') as ofile: