标签:rip not submit exists img 含义 com dir sub
wd=“要搜索的内容”
q=“要搜索的内容”
params
参数。只须构造键值对,提交给params
即可。>>> import requests
>>> kv = {‘wd‘:‘python‘}
>>> r = requests.get("http://www.baidu.com/s",params=kv)
>>> r.status_code
200
response
对象中的request
对象来查看我们提交给服务器的URL链接。>>> r.request.url
‘http://www.baidu.com/s?wd=python‘
import requests
kv = {‘wd‘:‘python‘}
url = "http://www.baidu.com/s"
try:
r = requests.get(url,params = kv)
print(r.request.url)
r.raise_for_status()
print(r.text[:2000])
except:
print("爬取失败")
url+xxxx.jpg
>>> import requests
>>> path = "/Users/hyliu/Desktop/picture.jpg"
>>> url = "https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=3acf58502f7f9e2f6438155a7e598241/7aec54e736d12f2e227c44d647c2d5628535680f.jpg"
>>> r = requests.get(url)
>>> r.status_code
200
r
中已经包含了我们想要的图片,接下来要做的事情就是把图片变成文件存放到本机。>>> path = "/Users/hyliu/Desktop/picture.jpg"
>>> with open(path,‘wb‘) as f:
f.write(r.content)
53658
>>>
picture.jpg
,并定义为一个文件标识符f
。然后我们将返回的内容写入到这个文件中。r.content
表示返回内容的二进制格式,所以我们将r.content
写入到图片文件中。>>> f.close()
>>>
import requests
import os
url = "https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=3acf58502f7f9e2f6438155a7e598241/7aec54e736d12f2e227c44d647c2d5628535680f.jpg"
root = "/Users/hyliu/Desktop/"
path = root + url.split(‘/‘)[-1] #获取URL最后一个“/”后的内容,实际上就是获取图片原本的名字
try:
if not os.path.exists(root): #目录不存在则创建
os.mkdir(root)
if not os.path.exists(path): #判断是否存在重名文件
r = requests.get(url)
#print(r.status_code)
r.raise_for_status()
with open (path,‘wb‘) as f:
f.write(r.content)
f.close()
print("文件保存成功!")
else:
print("文件已存在")
except:
print("爬取失败")
>>> import requests
>>> url = "http://m.ip138.com/ip.asp"
>>> kv = {‘ip‘:‘218.106.145.15‘}
>>> r = requests.get(url,params = kv)
>>> r.status_code
200
>>> r.text[-500:]
‘submit" value="查询" class="form-btn" />\r\n\t\t\t\t\t</form>\r\n\t\t\t\t</div>\r\n\t\t\t\t<div class="query-hd">ip138.com IP查询(搜索IP地址的地理位置)</div>\r\n\t\t\t\t<h1 class="query">您查询的IP:218.106.145.15</h1><p class="result">本站主数据:福建省福州市 联通</p><p class="result">参考数据一:福建省福州市 联通</p>\r\n\r\n\t\t\t</div>\r\n\t\t</div>\r\n\r\n\t\t<div class="footer">\r\n\t\t\t<a href="http://www.miitbeian.gov.cn/" rel="nofollow" target="_blank">沪ICP备10013467号-1</a>\r\n\t\t</div>\r\n\t</div>\r\n\r\n\t<script type="text/javascript" src="/script/common.js"></script></body>\r\n</html>\r\n‘
>>>
import requests
kv = {‘ip‘:‘218.106.145.15‘}
url = "http://m.ip138.com/ip.asp"
try:
r = requests.get(url,params = kv)
r.raise_for_status()
print(r.text[-500:])
except:
print("爬取失败")
URL is API
标签:rip not submit exists img 含义 com dir sub
原文地址:https://www.cnblogs.com/031602523liu/p/9824799.html