抱歉,我对编码不熟悉,但我真的很想学习。我无法传递我想要的值,在这种情况下,这只是我想从网络上抓取的数字。问题是我无法将我用 beautifulsoup 抓取的值传递到 csv 文件,任何好心人可以帮助我吗?from bs4 import BeautifulSoupfrom urllib.request import Request, urlopenimport csvreq = Request('https://www.infodolar.com.mx/tipo-de-cambio-dof-diario-oficial-de-la-federacion.aspx', headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'})html_page = urlopen(req).read()soup = BeautifulSoup(html_page,'html.parser')Dollar = soup.find('td','colCompraVenta')print (Dollar.text)file=open('New.csv', 'wb')writer = csv.writer(file)writer.writerows([Dollar.encode('utf-8')])file.close()
1 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
将此行更改为file=open('New.csv', 'wb')您file=open('New.csv', 'w') 的代码将如下所示
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import csv
req = Request('https://www.infodolar.com.mx/tipo-de-cambio-dof-diario-oficial-de-la-federacion.aspx', headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36'})
html_page = urlopen(req).read()
soup = BeautifulSoup(html_page,'html.parser')
Dollar = soup.find('td','colCompraVenta')
print (Dollar.text)
file=open('New.csv', 'w')
writer = csv.writer(file)
writer.writerows([Dollar.encode('utf-8')])
file.close()
添加回答
举报
0/150
提交
取消