# 使用import导入requests模块
import requests
# 使用from...import从bs4模块中导入BeautifulSoup
from bs4 import BeautifulSoup
# 使用import导入json模块
import json
# 复制网页链接赋值给变量url
url = "https://search.jd.com/Search?keyword=%E5%B7%A7%E5%85%8B%E5%8A%9B&enc=utf-8&wq=%E5%B7%A7%E5%85%8B%E5%8A%9B&pvid=dd65926ff30441409a11eceb998167db"
# 将User-Agent以字典键值对形式赋值给headers
# 将cookie以字典键值对形式赋值给headers
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
"cookie":"shshshfp=ad8b6f6f873021ea27d27b375521e98b; shshshfpa=9ef2cc10-dc6d-8605-51d3-f11877d56f0c-1679714667; shshshfpx=9ef2cc10-dc6d-8605-51d3-f11877d56f0c-1679714667; __jdv=76161171|direct|-|none|-|1702984948874; __jdu=17029849488732076901890; areaId=3; ipLoc-djd=3-51045-0-0; PCSYCityID=CN_120000_120100_0; mba_muid=17029849488732076901890; wlfstk_smdl=noteffw1kalwzwwbis2yt4s6wr9kk937; TrackID=18w9MqdRYteU_pz-D90f-47Ch3voih0rU5tERbjKmoLpr0419cBSBERWztCGd1gl5khh7u5Uwg6jdcprOZ5g0t2cOqg3EZFVCGqUZS4s9GE2WpXKB0JWbvIQhKZOTZbgw; thor=7500AD130E83509C62A128D5508603B90ACF01C2C444EC9CA6557FF761C103B36A8AFDDACB261F1BDF9DD476F195308104BA2F25F8B51942868DD3F77222C8D7B968E169810A697A465721A9CF5CBA0AC1E32A1FF77A9932592A6B78ADEEF5F6087B0CB9BAEC48037AD19F9B783CC2BE37B7F469B269B7D26922D25EE65CA068240443DB9677BF0E6040494F69851A084DD6FFB190FA24714D8D5D581F4A3B34; flash=2_p7efDyLrCjkzmvakxfopt0ZxYtcKCl5EiADf8eDmJyFXOERNdKPhB8RL3C--8KYBFNpnEDDTKiDRXBxcjR-dUjnhsO9AvuHiGAfcMFgJ8Uh*; pinId=byxrl4hSlZ5HQgI0j6fd-A; pin=jd_NRxfoeEnUwOH; unick=jd_NRxfoeEnUwOH; ceshi3.com=000; _tp=8PuLVIpgrdeqyf3RnhyC1w%3D%3D; _pst=jd_NRxfoeEnUwOH; jsavif=0; jsavif=0; rkv=1.0; __jda=143920055.17029849488732076901890.1702984949.1702984949.1702984949.1; __jdc=143920055; xapieid=jdd0373NT7KIRWYCKGAO3DIPVGM5SXIVZXBUBZI65JYVTVF4QQTUD23SVVVZZDY537K2TAGJKVWK4BXH6WROJRK6KCSHGSQAAAAMMQHQ7A3QAAAAACQUNGLTYZMFALIX; __jdb=143920055.6.17029849488732076901890|1.1702984949; shshshsID=a1eec9ee956c56705d157f92f26d56f6_4_1702986674501; 3AB9D23F7A4B3CSS=jdd0373NT7KIRWYCKGAO3DIPVGM5SXIVZXBUBZI65JYVTVF4QQTUD23SVVVZZDY537K2TAGJKVWK4BXH6WROJRK6KCSHGSQAAAAMMQHVG7JIAAAAACW3WZMR7TADAIQX; shshshfpb=AAhpw6oGMEvLMENxthgVR0_EYd9VvDBZ5cUZnQwAAAAA; qrsc=3; 3AB9D23F7A4B3C9B=73NT7KIRWYCKGAO3DIPVGM5SXIVZXBUBZI65JYVTVF4QQTUD23SVVVZZDY537K2TAGJKVWK4BXH6WROJRK6KCSHGSQ"}
# 使用get()函数请求链接,并且带上headers
response = requests.get(url, headers=headers)
# 使用.text属性将服务器相应内容转换为字符串形式,赋值给html
html = response.text
# 使用BeautifulSoup()传入变量html和解析器lxml,赋值给soup
soup = BeautifulSoup(html, "lxml")
# 使用find_all()查询soup中class="gl-item"的节点,赋值给content_all
content_all = soup.find_all(class_="gl-item")
# 使用for循环遍历content_all
for content in content_all:
# 使用.attrs属性获取data-sku对应的属性值,并赋值给p_id
p_id = content.attrs["data-sku"]
# 取出每个商品编号,用格式化字符串的方式,拼接出新的链接,赋值给url
url = f"https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId={p_id}&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&rid=9d8a323c646add49&fold=1"
# 使用get()函数请求链接,并且带上headers,赋值给res
res = requests.get(url, headers=headers)
# 使用.text属性将响应消息转换成字符串,赋值给html
html = res.text
# 使用lstrip()移除左侧的"fetchJSON_comment98(",赋值给html
html = html.lstrip("fetchJSON_comment98(")
# 使用rstrip()移除右侧的);,赋值给html
html = html.rstrip(");")
# 使用json.loads()将str转换成dict型,赋值给json_data
json_data = json.loads(html)
# 使用"hotCommentTagStatistics"键获取对应的值,赋值给data
data = json_data["hotCommentTagStatistics"]
# 定义一个字典p_dict用于存放每个商品的标签信息
p_dict = {}
# 使用for循环遍历列表data中的每一项
for item in data:
# 提取键name对应的值,赋值给name
name = item["name"]
# 提取count对应的值,赋值给count
count = item["count"]
# 按评价:数量 的方式写入到字典中
p_dict[name] = count
# 使用with...as语句配合open()函数打开文件商品信息.txt
# 将打开的文件设置为f
with open("商品信息.txt","a",encoding="utf-8") as f :
# 使用write()函数写入商品信息
# 使用str()将字典转为字符串格式
# 每写完一个换行"\n"
f.write(str(p_dict)+"\n")
以上是源代码,但是文件中还是一部分乱码