#!/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')