#!/usr/bin/env python
# -*- coding:utf-8 -*-
# import urllib
# import urllib2
# import urllib3
# import http
import requests
# 服务器向客户端返回的数据格式有哪些?
# JSON/XML
# 1. 使用requests发送get/post/put/delete等请求
# GET参数
# URL?参数1=内容1&参数2=内容2....
# 注意:参数部分不能出现空格或者特殊字符
response = requests.get("http://api.map.baidu.com/telematics/v3/weather?location=郑州市&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?")
# print(response.content)
response = requests.get("https://www.baidu.com/s?wd=python")
# print(response.content)
response = requests.get(
"https://www.baidu.com/s",
params={
"wd": "python"
}
)
# print(response.content)
# 参数1:url
# 参数2:data,类似于params
# 参数3:json
# 参数4:**kwargs
response = requests.post(
"http://dig.chouti.com/login",
data={
"phone": "8615896901897",
"password": "qweqweqwe1",
"oneMonth": "1"
},
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0",
}
)
# f = open("chouti.html", "wb")
# f.write(response.content)
# f.close()
print(response.text)
# response = requests.put()
# response = requests.delete()
# 举例
#查询歌曲
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import json
import os
while True:
name = input("歌曲名称:")
if not name.strip():
break
if not os.path.exists('name.txt'):
with open('name.txt', 'w', encoding='utf-8') as f:
f.write('')
with open('name.txt', 'r', encoding='utf-8') as f:
name_list = f.readlines()
if name+"\n" in name_list:
print('已经搜索过')
continue
else:
with open('name.txt', 'a', encoding='utf-8') as f:
f.write(name)
f.write('\n')
start_page = 0
num = 25
url = "http://search.kuwo.cn/r.s?ft=music&itemset=web_2013&client=kt&rformat=json&encoding=utf8"
response = requests.get(
url=url,
params={
"all": name,
"pn": start_page,
"rn": num
}
)
# JSON的key和value不能用单引号括起来
result = response.text.replace("'", '"').replace(' ', '')
json_obj = json.loads(result)
song_list = json_obj['abslist']
for song in song_list:
# print(song.get('SONGNAME', '没有歌曲名称'))
# f = open("song.txt", 'a', encoding='utf-8')
# f.write(song.get('SONGNAME', '没有歌曲名称'))
# f.write('\n')
# f.close()
with open("song.txt", 'a', encoding='utf-8') as f:
f.write(song.get('SONGNAME', '没有歌曲名称'))
f.write('\n')