保存网页视频或图片
import requests
import os
url = "http://sp.jzsc.net/upload/video/2019-05-29/a91a6577-b330-4f37-8509-35e40f28e5bd.mp4"
root = "D://pics//"
path = root + url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(url)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print("文件已存储")
else:
print("文件已存在")
except:
print("爬取失败")
获取百度搜索内容
import requests
url = "http://www.baidu.com/s"
try:
kv = {'wd':'python'}
r = requests.get(url,params=kv)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.request.url)
except:
print("爬取失败")