标签:baidu with dem des .text mkdir try ati 爬取
1、搜索结果爬取(未解析)
#coding:utf-8 import requests url="http://www.baidu.com/s" try: #kv={‘user-agent‘:‘Mozilla/5.0‘} kv={‘wd‘:‘Python‘} r=requests.get(url,params=kv) print r.status_code r.raise_for_status() r.encoding=r.apparent_encoding print len(r.text) except: print ‘产生异常‘
2、爬取图片
#coding:utf-8 import requests import os url="http:image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg" root="/Users/wangkun/Desktop/DEMO1/" 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 ‘爬取失败‘
3、IP归属地查询
#coding:utf-8 import requests url="http://m.ip138.com/ip.asp?ip=" try: r=requests.get(url+‘202.204.80.112‘) r.raise_for_status() r.encoding=r.apparent_encoding print r.text[-500:] except: print ‘爬取失败‘
标签:baidu with dem des .text mkdir try ati 爬取
原文地址:https://www.cnblogs.com/elpsycongroo/p/9452269.html