import requests
import json
if __name__='__main__':
word=input()
post_url='url'
data={
'kw':word
}
headers={
'User-Agent':'Mozilla/5.0 (Macintsh; Inte Mac OS X 1_15_7)'}
res=requests.post(url=post_url,data=data,headers=headers)
dic_obj=res.json()#拿到json类型数据
fp=open(f'./{
word}.json','w',encoding='utf-8')
json.dump(dic_obj,fp=fp,ensure_ascii=False)
下载与批量下载
掌握request =掌握爬虫50%
import requests
#第三方库,没有下载的下载一下 pip install requests#爬虫下载图片
res=requests.get("url")print(res.content)#二进制字节流#content-type为text/plain的也是用res.text获取print(res.text)#获取网页 字符串类型数据及html源码#写文件withopen("beauty.jpg","wb")as f:
f.write(res.content)#批量下载短视频
headers={
'User-Agent':'Mozilla/5.0 (Macintsh; Inte Mac OS X 1_15_7)'}#UA伪装#get->params#post->data
params={
'k':'v'}
res=requests.get("url/post/",params=params,headers=headers)print(res.json