代码
import requests
import csv
def get_data():
url = 'https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1708743664770&countryId=&cityId=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=python&pageIndex=1&pageSize=10&language=zh-cn&area=cn'
res = requests.get(url)
items = res.json()
datas = list()
for data in items.get('Data')['Posts']:
RecruitPostName = data.get('RecruitPostName')
LocationName = data.get('LocationName')
Responsibility = data.get('Responsibility')
datas.append([RecruitPostName, LocationName, Responsibility])
with open('tx.csv', 'w', encoding='utf-8', newline='') as f:
writer = csv.writer(f);
# 写一个头部
writer.writerow(['岗位', '地区', '简介'])
# 开始写数据
writer.writerows(datas)
print('数据写入完毕!')
if __name__ == '__main__':
get_data()
问题描述
在PyCharm编辑器中,打开是正常的,
直接打开csv文件,却是乱码一通。
解决方案
在open()函数中将编码方式修改为encoding='utf-8-sig'
,即可解决。