Попытка извлечь данные из таблицы, и есть иностранные символы, мешающие мне писать в файл CSV - PullRequest
0 голосов
/ 01 мая 2019

Я извлекаю данные, но некоторые специальные символы будут вызывать ошибку

from unicodedata import normalize


import codecs
import csv
import urllib2
import requests
from BeautifulSoup import BeautifulSoup

url = 'https://www.ratebeer.com/top'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html)
table = soup.find('tbody')

list_of_rows = []


for row in table.findAll('tr'):
list_of_cells = []
    for cell in row.findAll('td'):
        text = cell.text
        list_of_cells.append(text)
    list_of_rows.append(list_of_cells)

outfile = open("./top50.csv", "wb")
writer = csv.writer(outfile)
writer.writerows(list_of_rows)

при попытке извлечь CSV-файл для импорта в Excel с 50 лучшими пивом, рангом, именем, стилем, пивоварней, рейтингом

Ответы [ 2 ]

0 голосов
/ 01 мая 2019

Рассмотрите возможность использования панд? Вы можете указать кодировку, которая обрабатывает символы encoding='utf-8-sig'.

import pandas as pd
import requests
r = requests.get('https://www.ratebeer.com/top', headers = {'User-Agent' : 'Mozilla/5.0'})
table = pd.read_html(r.text)[0]
table.drop(['Unnamed: 5'], axis=1, inplace = True)
table.columns = ['Rank', 'Name', 'Count', 'Abv', 'Score']
table.to_csv(r"C:\Users\User\Desktop\Data.csv", sep=',', encoding='utf-8-sig',index = False ) 
0 голосов
/ 01 мая 2019

Это работает, python 3.6, определенный синтаксический анализатор features="lxml" и кодировка encoding='utf-8':

import codecs, csv, urlib, requests
from unicodedata import normalize
from bs4 import BeautifulSoup

url = 'https://www.ratebeer.com/top'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html, features="lxml")
table = soup.find('tbody')

list_of_rows = []

for row in table.findAll('tr'):
    list_of_cells = []
    for cell in row.findAll('td'):
        text = cell.text
        list_of_cells.append(text)
    list_of_rows.append(list_of_cells)

outfile = open("./top50.csv", "w", encoding='utf-8')
writer = csv.writer(outfile)
writer.writerows(list_of_rows)
...