Python 爬虫:2.几个爬虫实例
2018-02-27 11:39
337 查看
#百度搜索全代码 import requests try: kv={'wd':'Python'} r=requests.get('http://www.baidu.com/s',params=kv) print(r.request.url) r.raise_for_status() print(len(r.text)) except: print('ERROR') #爬取搜狗搜索全代码 import requests try: kv={'query':'Python'} r=requests.get('http://www.sogou.com/web',params=kv) print(r.request.url) r.raise_for_status() print(len(r.text)) except: print('ERROR') #爬取网页上的图片 import requests path='D:/abc.jpg' url='https://p1.ssl.qhmsg.com/t01b91ce68adb052a9e.jpg' r=requests.get(url) print(r.status_code) with open(path,'wb') as f: f.write(r.content) f.close() import requests path='D:/a.png' url='http://p0.ifengimg.com/pmop/2018/0227/13CB6A9806BFBFA31E2D3EE039923442A9669FD0_size25_w318_h300.png' r=requests.get(url) with open(path,'wb') as f: f.write(r.content) f.close() #爬取网页上的图片,并用原本的名称命名 import requests import os url='https://p1.ssl.qhmsg.com/t01b91ce68adb052a9e.jpg' 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('Success!') else: print('Already exits') except: print('ERROR')
结果:
http://www.baidu.com/s?wd=Python 334987 http://www.sogou.com/web?query=Python 313179 200 Success!
相关文章推荐
- 几个python简单爬虫实例
- python3下几个淘宝、天猫、京东爬虫实例。(价格、销量、评论等)
- Python爬虫实例——基于BeautifulSoup和requests实现
- Python requests爬虫实例
- python爬虫实例项目大全
- Python网络爬虫与信息提取-Day14-(实例)股票数据定向爬虫
- Python爬虫实例扒取2345天气预报
- Python 爬虫多线程详解及实例代码
- Python3多线程爬虫实例讲解代码
- Python开发实例分享bt种子爬虫程序和种子解析
- python爬虫慕课网实例(一)
- Python---BeautifulSoup 简单的爬虫实例
- Python爬虫----实例: 抓取百度百科Python词条相关1000个页面数据
- Python爬虫包 BeautifulSoup 学习(三) 实例
- python3.5爬虫实例:根据城市名称来获取该城市最近七天的天气预报
- Python 爬虫实例(3)—— 爬取今日头条as cp 算法 解密
- Python爬虫包 BeautifulSoup 学习(五) 实例
- python项目之爬虫实例(一)
- python下的一些特殊属性(几个实例说明)
- Python爬虫框架Scrapy实例代码