# -*- coding:utf-8 -*- import os import requests input_name = input("请输入你要下载的歌曲或歌手:") url = "http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key={}&pn=1&rn=30&httpsStatus=1&reqId=64b76101-9883-11ec-9b9e-2f1fed2b10cf".format(input_name) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36', 'Cookie': 'Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1651235981; _ga=GA1.2.593351211.1651235981; _gid=GA1.2.1236513393.1651235981; _gat=1; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1651236048; kw_token=44Y6M2EQ515', 'csrf': '44Y6M2EQ515', 'Host': 'www.kuwo.cn', 'Referer': 'http://www.kuwo.cn/search/list?key=%E5%AD%A4%E5%8B%87%E8%80%85' } music_list = requests.get(url=url, headers=headers).json()["data"]["list"] music_rid_list = [] for music in music_list: music_rid = music["rid"] music_name = music["name"] music_artist = music["artist"] #print(music_rid) music_url = f'http://www.kuwo.cn/api/v1/www/music/playUrl?mid={music_rid}&type=convert_url3&br=320kmp3' #print(music_url) download_url = requests.get(music_url).json()["data"]["url"] music = requests.get(download_url).content if not os.path.exists(r"./music"): os.mkdir(r"./music") else: with open(f'./music/{music_name}.mp3', mode="wb") as f: f.write(music) print(f"{music_name},下载成功!")
时间: 2024-04-17 11:24:05 浏览: 238
根据你提供的代码,你尝试从返回的JSON数据中获取键为 'data' 的值,然后进一步获取键为 'list' 的值。但是,如果返回的JSON数据中没有这样的键或者键的结构不同,那么就会触发 KeyError 错误。
为了解决这个问题,你可以在访问JSON数据之前,先打印输出一下这个返回的JSON数据,看看它的结构是怎样的。可以使用以下代码:
```python
response = requests.get(url=url, headers=headers)
data = response.json()
print(data)
```
这样可以查看实际返回的JSON数据的结构。确认正确的键和值之后,再进行后续的操作。
另外,也要确保请求的URL和请求头信息都是正确的。如果有必要,可以在浏览器中手动复制请求URL并查看响应结果,以确保URL和头信息是正确的。
如果你仍然遇到问题,请提供打印输出的JSON数据,我可以帮助你进一步分析。
相关问题
帮我将以下代码写注释# coding=gbk # -- coding:uft-8 -- # BOSS直聘 import requests from time import sleep import csv def collect(page): url = f'https://www.zhipin.com/wapi/zpgeek/search/joblist.json' headers = { 'accept': 'application/json, text/plain, /', 'cookie': ck, 'referer': 'https://www.zhipin.com/web/geek/job?query=%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%BD%AF%E4%BB%B6&city=101190100', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36', 'x-requested-with': 'XMLHttpRequest' } params = { 'scene': '1', 'query': '计算机软件', 'city': '100010000', 'experience': '', 'degree': '', 'industry': '', 'scale': '', 'stage': '', 'position': '', 'salary': '', 'multiBusinessDistrict': '', 'page': page, 'pageSize': '30' } res = requests.get(url=url, headers=headers, params=params).json() for li in res['zpData']['jobList']: jobName = li['jobName'] brandName = li['brandName'] cityName = li['cityName'] areaDistrict = li['areaDistrict'] businessDistrict = li['businessDistrict'] salaryDesc = li['salaryDesc'] jobExperience = li['jobExperience'] jobDegree = li['jobDegree'] skills = '/'.join(li['skills']) brandIndustry = li['brandIndustry'] welfareList = '/'.join(li['welfareList']) row = [jobName, brandName, cityName, areaDistrict, businessDistrict, salaryDesc, jobExperience, jobDegree, skills, brandIndustry, welfareList] print(row) csv.writer(open('BOSS直聘.csv', 'a', encoding='utf-8-sig', newline='')).writerow(row) if name == 'main': ck = '' csv.writer(open('BOSS直聘.csv', 'w', encoding='utf-8-sig', newline='')).writerow( ['jobName', 'brandName', 'cityName', 'areaDistrict', 'businessDistrict', 'salaryDesc', 'jobExperience', 'jobDegree', 'skills', 'brandIndustry', 'welfareList']) for p in range(10): p += 1 while True: try: collect(p) break except: ck = input('请输入cookie: ') print(f'page {p} finish!') sleep(5)
# coding=gbk
# -- coding:utf-8 --
# BOSS直聘
import requests
from time import sleep
import csv
# 定义函数collect,用于收集每一页的数据
def collect(page):
# 设置请求的URL和headers
url = f'https://www.zhipin.com/wapi/zpgeek/search/joblist.json'
headers = {
'accept': 'application/json, text/plain, /',
'cookie': ck, # 这里的ck需要用户输入
'referer': 'https://www.zhipin.com/web/geek/job?query=%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%BD%AF%E4%BB%B6&city=101190100',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'x-requested-with': 'XMLHttpRequest'
}
# 设置请求的params参数
params = {
'scene': '1',
'query': '计算机软件',
'city': '100010000',
'experience': '',
'degree': '',
'industry': '',
'scale': '',
'stage': '',
'position': '',
'salary': '',
'multiBusinessDistrict': '',
'page': page,
'pageSize': '30'
}
# 发送请求并获取返回的JSON数据
res = requests.get(url=url, headers=headers, params=params).json()
# 遍历每个职位信息,并将其写入CSV文件
for li in res['zpData']['jobList']:
jobName = li['jobName']
brandName = li['brandName']
cityName = li['cityName']
areaDistrict = li['areaDistrict']
businessDistrict = li['businessDistrict']
salaryDesc = li['salaryDesc']
jobExperience = li['jobExperience']
jobDegree = li['jobDegree']
skills = '/'.join(li['skills'])
brandIndustry = li['brandIndustry']
welfareList = '/'.join(li['welfareList'])
row = [jobName, brandName, cityName, areaDistrict, businessDistrict, salaryDesc, jobExperience, jobDegree, skills, brandIndustry, welfareList]
print(row) # 打印每个职位信息
csv.writer(open('BOSS直聘.csv', 'a', encoding='utf-8-sig', newline='')).writerow(row) # 将职位信息写入CSV文件
# 如果该脚本作为主程序运行,则从这里开始执行
if __name__ == 'main':
ck = '' # 定义Cookie初始值为空
# 在CSV文件中写入表头
csv.writer(open('BOSS直聘.csv', 'w', encoding='utf-8-sig', newline='')).writerow(
['jobName', 'brandName', 'cityName', 'areaDistrict', 'businessDistrict', 'salaryDesc', 'jobExperience', 'jobDegree', 'skills', 'brandIndustry', 'welfareList'])
# 循环收集前10页的职位信息
for p in range(10):
p += 1
while True:
try:
collect(p) # 收集每一页的职位信息
break
except:
ck = input('请输入cookie: ') # 如果出现错误,需要用户重新输入Cookie
print(f'page {p} finish!') # 打印完成一页的信息
sleep(5) # 暂停5秒,避免请求过于频繁
基于python做一个亚马逊和京东的比价系统,在系统界面上有待比价商品输入框,和比价按钮。可以选择在亚马逊和京东两个网站上检索商品。在flask web程序中控制比价流程,并返回比价结果。比价结果网页中展示出来,请将代码写出来
好的,下面是一个简单的实现代码,供您参考:
```python
# coding:utf-8
import requests
from bs4 import BeautifulSoup
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
# 创建数据库
conn = sqlite3.connect('product.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS product
(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, price FLOAT, link TEXT)''')
conn.commit()
# 爬取亚马逊商品信息
def crawl_amazon_product(name):
url = 'https://www.amazon.com/s?k=' + name
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
products = soup.find_all('div', {'class': 's-result-item'})
for product in products:
try:
name = product.find('h2', {'class': 'a-size-mini'}).text.strip()
price = float(product.find('span', {'class': 'a-price-whole'}).text.strip().replace(',', ''))
link = 'https://www.amazon.com' + product.find('a', {'class': 'a-link-normal'})['href']
c.execute("INSERT INTO product (name, price, link) VALUES (?, ?, ?)", (name, price, link))
conn.commit()
except:
continue
# 爬取京东商品信息
def crawl_jd_product(name):
url = 'https://search.jd.com/Search?keyword=' + name
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
products = soup.find_all('div', {'class': 'gl-i-wrap'})
for product in products:
try:
name = product.find('div', {'class': 'p-name'}).text.strip()
price = float(product.find('div', {'class': 'p-price'}).strong.i.text)
link = 'https:' + product.find('div', {'class': 'p-name'}).a['href']
c.execute("INSERT INTO product (name, price, link) VALUES (?, ?, ?)", (name, price, link))
conn.commit()
except:
continue
# 比价函数
def compare(name, site):
# 先清空数据库
c.execute("DELETE FROM product")
conn.commit()
# 根据选择的网站爬取商品信息
if site == 'amazon':
crawl_amazon_product(name)
elif site == 'jd':
crawl_jd_product(name)
else:
return []
# 从数据库中获取比价结果
c.execute("SELECT name, price, link FROM product ORDER BY price")
result = c.fetchall()
return result
# 网页界面
@app.route('/')
def index():
return render_template('index.html')
# 比价操作
@app.route('/compare', methods=['POST'])
def compare_products():
name = request.form['name']
site = request.form['site']
result = compare(name, site)
return render_template('result.html', result=result)
if __name__ == '__main__':
app.run(debug=True)
```
其中,`crawl_amazon_product()` 和 `crawl_jd_product()` 函数分别用于爬取亚马逊和京东的商品信息,并将爬取到的商品信息存储到数据库中。`compare()` 函数则用于比价操作,根据选择的网站爬取商品信息,然后从数据库中获取比价结果。
在网页界面中,我们提供了一个待比价商品输入框和一个下拉菜单,用户可以在输入框中输入待比价的商品名称,选择需要比价的网站(亚马逊或者京东),点击比价按钮后,程序会从数据库中获取对应的商品信息,并进行比价操作,最终返回比价结果。
具体的网页界面和展示结果的代码可以参照以下示例:
```html
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>比价系统</title>
</head>
<body>
<h1>比价系统</h1>
<form method="POST" action="/compare">
<label for="name">待比价商品:</label>
<input type="text" name="name" id="name">
<br>
<label for="site">选择网站:</label>
<select name="site" id="site">
<option value="amazon">亚马逊</option>
<option value="jd">京东</option>
</select>
<br>
<button type="submit">比价</button>
</form>
</body>
</html>
```
```html
<!-- result.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>比价结果</title>
</head>
<body>
<h1>比价结果</h1>
<table>
<thead>
<tr>
<th>商品名称</th>
<th>价格</th>
<th>链接</th>
</tr>
</thead>
<tbody>
{% for item in result %}
<tr>
<td>{{ item[0] }}</td>
<td>{{ item[1] }}</td>
<td><a href="{{ item[2] }}" target="_blank">{{ item[2] }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
```
以上代码仅供参考,具体的实现细节可能会有所不同,需要根据实际情况进行调整。
阅读全文