Get发送内容格式
Get方式主要需要发送headers、url、cookies、params等部分的内容。
t = requests.get(url, headers = header, params = content, cookies = newscookies)
基本上发送以上四个变量即可,以下是示例代码。
url = 'https://weibo.com/a/aj/transform/loadingmoreunlogin' content = { 'ajwvr': 6, 'category': 1760, 'page': 3, 'lefnav': 0 } header = { 'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1', 'Accept': r'*/*', 'Accept-Language': r'en-US,en;q=0.5', 'Accept-Encoding': r'gzip, deflate', 'Referer': referer, 'DNT': '1', 'Connection': r'keep-alive' } # "\" 字符可以起到代码换行的作用 newscookies = \ { "Apache": "8599973819110.777.1525849965283", "SINAGLOBAL": "8599973819110.777.1525849965283" } t = requests.get(url, headers = header, params = content, cookies = newscookies)
print(t.text)
处理JSON文件
主要思路将JSON文件转化为Python字典变量,二者的形式类似。
处理时注意JSON文件中可能同时包含列表List,有时需要指定下标,提取字典。
json.loads()
该函数将str类型转换为dict类型,其中字典中的引号为双引号。
p = '''{"a": 1, "b": 2}''' q = json.loads(p)
json.dumps()
该函数将dict类型的数据转换为str
p = {"a": 1, "b": 2} q = json.dumps(p)
通过DataFrame保存为xlsx
位于pandas库中的dataframe用法有很多,这里只举一个例子,就是将列表组合成字典,存成dataframe,最后保存xlsx。
labelFrame = { 'Date': newDate, 'UsefulCount': newUseful, 'ServeScore': newScoreA, 'PlayScore': newScoreB } p = pd.DataFrame(labelFrame) p.to_excel('a.xlsx')