-1

Im sorry im new to coding but im really trying to learn.Im not able to pass the value i want, wich in this case is just a number i want to scrape from the web.

The problem it's that im not able no pass the value i scrape with beatifulsoup to a csv file,can any kind person help me with this?

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', 'wb')
writer = csv.writer(file)





writer.writerows([Dollar.encode('utf-8')])

file.close()
  • Does this answer your question? [TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3](https://stackoverflow.com/questions/33054527/typeerror-a-bytes-like-object-is-required-not-str-when-writing-to-a-file-in) – msanford Oct 20 '20 at 14:22
  • To help you out, you must *include debugging details in your question.* Notably, the error you get: `TypeError: a bytes-like object is required, not 'str'` If you had put this, it would have suggested the above duplicate, or you could google the error. – msanford Oct 20 '20 at 14:23
  • Thanks men some already answer my question, the prblem was in this line of code,file=open('New.csv', 'w') , i was using 'wb' instead of 'w' now i understand that i need this to use external data from web in order to pass it correctly to a cvs file. – Eddy Jackson Oct 20 '20 at 14:54

1 Answers1

0

change this line to file=open('New.csv', 'wb') to file=open('New.csv', 'w') your code will looks like the following

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()
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
  • It's usually a good idea to _explain why_ this will fix the problem, otherwise people will just copy-paste without understanding... Notably: https://stackoverflow.com/questions/2665866/what-does-wb-mean-in-this-code-using-python – msanford Oct 20 '20 at 14:24
  • I'm sorry didn't about that question – Umutambyi Gad Oct 20 '20 at 14:26
  • 1
    Thanks man now the proggram runs with no issues on the prompt command, but the only thing i need no find now its to use the writer.writerows([Dollar.encode('utf-8')]) correctly for the proggram to be able to write that value to the cvs, thank you so much by the way for the directions men, it was frustating cause i didn't knew what i was doing wrong on with that specific problem,God bless you men i hope i can tackle this challenge, and i hope your reputation as a good programer grows up, Thanks man. – Eddy Jackson Oct 20 '20 at 14:47