网站爬取
import requests
url = "https://www.bilibili.com/?spm_id_from=666.6.b_62616e6e65725f6c696e6b.12"
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.text)
except:
print("fail")
import requests
url = "https://www.bilibili.com/?spm_id_from=666.6.b_62616e6e65725f6c696e6b.12"
try:
kv = {'user-agent':'Mozilla/5.0'}
r = requests.get(url,headers = kv)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.text)
except:
print("fail")
百度关键词爬取
import requests
keyword = "python"
url = "https://www.baidu.com/?tn=78040160_15_pg&ch=10"
try:
kv = {'wd': keyword}
r = requests.get(url,params=kv)
print(r.request.url)
r.raise_for_status()
print(len(r.text))
except:
print("fail")
图片爬取
import requests
import os
url = "http://a1.qpic.cn/psb?/V11vNoTN12ufkw/oiZzKcaCmIEP202YUz17*sdgN1gfVU4M0.Ke9emIlIc!/b/dOAAAAAAAAAA&ek=1&kp=1&pt=0&bo=7gI2Be4CNgURECc!&tl=3&vuin=2312365845&tm=1571742000&sce=60-4-3&rf=viewer_311"
root = "C://Users//Lenovo//Desktop//newfile//photo"
path = root + url.split('/')[-1]
try :
if not os.path.exists(root):
os.mkdir(path)
if not os.path.exists(path):
r = requests.get(url)
with open(path,'wb') as f:
f.write(r.content)
f.close();
print("successful")
else :
print("aleady exist")
except:
print("fail")
版权声明:本文为qq_42825058原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。