试图从表中提取数据,并且有外来字符阻止我写入csv文件

问题描述 投票:0回答:2

我正在提取数据,但一些特殊字符会导致错误

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顶级啤酒,排名,名称,风格,啤酒厂,评级

python python-2.7 web-scraping beautifulsoup non-ascii-characters
2个回答
0
投票

这是工作,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)

0
投票

考虑使用熊猫?您可以指定处理字符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 ) 
© www.soinside.com 2019 - 2024. All rights reserved.