websites - web scraping with python pdf
Come posso leggere queste celle da un codice html con il web-scraping di Python? (1)
Hai un problema di space
nel tuo codice dalla riga 18 for cell in row.findAll('td'):
alla riga 20 list_of_cells.append(text)
. Ecco il codice fisso:
import csv
import requests
from bs4 import BeautifulSoup
url = 'https://www.mnb.hu/arfolyamok'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)
table = soup.find('tbody', attrs={'class': 'stripe'})
table = str(soup)
table = table.split("<tbody>")
list_of_rows = []
for row in table[1].findAll('tr')[1:]:
list_of_cells = []
for cell in row.findAll('td'):
text = cell.text.replace(' ', '')
list_of_cells.append(text)
list_of_rows.append(list_of_cells)
print list_of_rows
outfile = open("./inmates.csv", "wb")
writer = csv.writer(outfile)
writer.writerow(["Pénznem", "Devizanév", "Egység", "Forintban kifejezett érték"])
writer.writerows(list_of_rows)
Ma, dopo aver eseguito questo codice, dovrai affrontare un altro problema, questo è un errore di codifica dei caratteri. Verrà letto " SyntaxError: Non-ASCII character '\xc3' in file testoasd.py on line 27, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
"
Come risolverlo? Abbastanza semplice ... aggiungi lo shebang # -*- coding: utf-8 -*-
nella parte superiore del tuo codice (1a riga). Dovrebbe risolverlo.
EDIT: Ho appena notato che stai usando BeautifulSoup in modo sbagliato e lo stai importando anche in modo errato. Ho corretto l'importazione from bs4 import BeautifulSoup
e quando si utilizza BeautifulSoup, è necessario specificare anche un parser. Così,
soup = BeautifulSoup(html)
potrebbe diventare :
soup = BeautifulSoup(html, "html.parser")
Voglio raschiare le informazioni sui prezzi di scambio da questo sito e dopo averlo inserito in un database: https://www.mnb.hu/arfolyamok
Ho bisogno di questa parte di html:
<tbody>
<tr>
<td class="valute"><b>CHF</b></td>
<td class="valutename">svájci frank</td>
<td class="unit">1</td>
<td class="value">284,38</td>
</tr>
<tr>
<td class="valute"><b>EUR</b></td>
<td class="valutename">euro</td>
<td class="unit">1</td>
<td class="value">308,54</td>
</tr>
<tr>
<td class="valute"><b>USD</b></td>
<td class="valutename">USA dollár</td>
<td class="unit">1</td>
<td class="value">273,94</td>
</tr>
</tbody>
Ecco perché ho scritto un codice, ma qualcosa di sbagliato in esso. Come posso aggiustarlo, dove devo cambiarlo? Ho bisogno solo dei dati "valute", "valutename", "unit" e "value". Sto lavorando con Python 2.7.13 su Windows 7.
Il messaggio di errore è il seguente: "C'è un errore nel tuo programma: unindent non corrisponde a nessun livello di indentazione esterno"
Il codice è qui:
import csv
import requests
from BeautifulSoup import BeautifulSoup
url = 'https://www.mnb.hu/arfolyamok'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)
table = soup.find('tbody', attrs={'class': 'stripe'})
table = str(soup)
table = table.split("<tbody>")
list_of_rows = []
for row in table[1].findAll('tr')[1:]:
list_of_cells = []
for cell in row.findAll('td'):
text = cell.text.replace(' ', '')
list_of_cells.append(text)
list_of_rows.append(list_of_cells)
print list_of_rows
outfile = open("./inmates.csv", "wb")
writer = csv.writer(outfile)
writer.writerow(["Pénznem", "Devizanév", "Egység", "Forintban kifejezett érték"])
writer.writerows(list_of_rows)