You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import requests
import json
from pprint import pprint
import csv
import os
import glob
f = open ( ' 评论.csv ' , mode = ' a ' , encoding = ' utf-8 ' , newline = ' ' )
csv_writer = csv . DictWriter ( f , fieldnames = [
' 昵称 ' ,
' 评论 ' ,
' 产品信息 ' ,
] )
csv_writer . writeheader ( )
#评论接口
url = ' '
headers = {
' Referer ' : ' https://detail.tmall.com/ ' ,
' cookie ' : ' ' ,
' user-agent ' : ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0 '
}
response = requests . get ( url = url , headers = headers )
#response_text = response.json()['comments']
#print(response.text)
for shuju in response . json ( ) [ ' data ' ] [ ' module ' ] [ ' reviewVOList ' ] :
dit = {
' 昵称 ' : shuju [ ' userNick ' ] ,
' 评论 ' : shuju [ ' reviewWordContent ' ] ,
' 产品信息 ' : shuju [ ' skuText ' ] ,
}
csv_writer . writerow ( dit )
def check_for_csv_files ( PY1 ) :
"""
检查指定目录下是否存在.csv文件。
:param directory: 要检查的目录路径
:return: 如果找到.csv文件返回True, 否则返回False
"""
# 使用glob模块匹配目录下的所有.csv文件
csv_files = glob . glob ( os . path . join ( PY1 , ' *.csv ' ) )
# 如果找到.csv文件, 则csv_files列表不为空, 返回True
return bool ( csv_files )
# 示例:检查当前目录下是否存在.csv文件
current_dir = os . getcwd ( )
if check_for_csv_files ( current_dir ) :
print ( " 存在.csv文件。 " )
import connect
connect . hello ( )
else :
print ( " 没有找到.csv文件。 " )
''' try:
json_str = response_text.strip()[len( ' mtopjsonp3( ' ):-1]
data = json.loads(json_str)
print(data)
except json.JSONDecodeError:
print( " JSON 解码失败,请检查响应是否为有效的 JSON 格式。 " ) '''