我尝试从 kickstarter 抓取数据,代码正在运行,但它在第 15 页出现以下错误(由于网页是动态的,您可能会在不同的页面中遇到错误):
回溯(最后一次调用):文件“C:\Users\lenovo\kick.py”,第 30 行,在 csvwriter.writerow(row) 文件“C:\Users\lenovo\AppData\Local\Programs\Python\Python37 \lib\encodings\cp1252.py",第 19 行,在编码返回 codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\uff5c' in position 27:字符映射到
可能是什么问题?有什么建议吗?
from urllib.request import urlopen
from bs4 import BeautifulSoup
import json
import csv
KICKSTARTER_SEARCH_URL = "https://www.kickstarter.com/discover/advanced?category_id=16&sort=newest&seed=2502593&page={}"
DATA_FILE = "kickstarter.csv"
csvfile = open(DATA_FILE, 'w')
csvwriter = csv.writer(csvfile, delimiter=',')
page_start = 0
while True:
url = KICKSTARTER_SEARCH_URL.format(page_start)
print(url)
response = urlopen(url)
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
project_details_divs = soup.findAll('div', {"class":"js-react-proj-card"})
if len(project_details_divs) == 0:
break;
for div in project_details_divs:
project = json.loads(div['data-project'])
row = [project["id"],project["name"],project["goal"],project["pledged"]]
csvwriter.writerow(row)
page_start +=1
csvfile.close()