自己爬虫新手实验
必应关键词搜索
import requests
try:
url="https://cn.bing.com/search"
keyword="计算机组成原理"
wk={'q':keyword}
r=requests.get(url,params=wk,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
print(r.text[:1000])
except IOError as e:
print(str(e))
timeout是请求时长
当当网书本
import requests
try:
url="https://product.dangdang.com/29526048.html"
kv={'User-Agent': 'Mozilla/5.0'}#构建hearders,没有这个会拒绝不合理的请求
r=requests.get(url,headers=kv,timeout=30)
r.raise_for_status()
r.encoding=r.apparent_encoding
print(r.text[:1000])
except IOError as e:
print(str(e))
网站图片下载
import requests
import os
url="https://alifei02.cfp.cn/creative/vcg/800/new/VCG41N1397194266.jpg"
dir="D://ai绘画//test//"
path=dir+url.split('/')[-1]#设置图片保存路径并以原图名名字命名
try:
if not os.path.exists(dir):
os.makedirs(dir)
if not os.path.exists(path):
r=requests.get(url)
# print(r.content)
with open(path,"wb") as f:
f.write(r.content)
f.close()
print("文件保存成功")
else:
print("文件已存在")
except IOError as e:
print(str(e))
post请求初尝试
import requests
import json
def baidutranslate(words=None):
url="https://fanyi.baidu.com/sug"
header={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
}
data={
"kw": words
}
res=requests.post(url,headers=header,data=data)
content=res.json()
print(content['data'][0]['v'])
if __name__=="__main__":
st=input("输入想翻译的")
baidutranslate(st)
标签:__,webcralertest,url,import,print,path,requests
From: https://www.cnblogs.com/minornerstop/p/17274138.html