我正在提取数据,但一些特殊字符会导致错误
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 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)
考虑使用熊猫?您可以指定处理字符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 )