python - 将特殊字符写入 csv 文件时出现问题
问题描述
我正在将网页的爬网输出写入 CSV 文件。但是,很少有特殊字符(例如“连字符”)无法正确解析。
原文:Amazon Forecast - 现已全面上市
csv 中的结果:Amazon Forecast – 现在普遍可用
我尝试了下面的代码
from bs4 import BeautifulSoup
from datetime import date
import requests
import csv
source = requests.get('https://aws.amazon.com/blogs/aws/').text
soup = BeautifulSoup(source, 'lxml')
# csv_file = open('aitrendsresults.csv', 'w')
csv_file = open('aws_cloud_results.csv', 'w' , encoding = 'utf8' )
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['title','img','src','summary'])
match = soup.find_all('div',class_='lb-row lb-snap')
for n in match:
imgsrc= n.div.img.get('src')
titlesrc= n.find('div',{'class':'lb-col lb-mid-18 lb-tiny-24'})
titletxt= titlesrc.h2.text
anchortxt= titlesrc.a.get('href')
sumtxt= titlesrc.section.p.text
print(sumtxt)
csv_writer.writerow([titletxt,imgsrc,anchortxt,sumtxt])
csv_file.close()
你能帮我得到与上面提供的原始文本相同的文本吗?
解决方案
I've been working with BS as well and I think you've only made a minor mistake. In line 8, where you open the csv file, the encoding should be "UTF-8" instead of "utf8". See if that helps.
推荐阅读
- java - 改进关于素数的代码的建议?
- neo4j - 如何在 SDN 5.x 中创建类似于 4.x 中的 Neo4jOperations 的动态密码查询
- angular - ./node_modules/@types/lodash/common/object.d.ts (1689,12):找不到名称“排除”
- json - 使用 pyspark 解析 JSON 时嵌套动态模式不起作用
- python - 如何将键与列python对齐?
- reactjs - 我不知道为什么我的应用不显示广告?
- android - 恢复db文件后重新打开房间数据库
- python - os.walk() 运行代码时不经过子目录
- python - 有什么方法可以在 pandas 中构建有效的特征矩阵以进行二元分类?
- php - 如何检索将值存储在php中嵌套数组中的json响应对象