python 爬虫demo
来源:互联网 发布:国际常用聊天软件 编辑:程序博客网 时间:2024/05/16 15:05
python 3.4 所写爬虫
仅仅是个demo,已百度图片首页图片为例。能跑出图片上的图片;
使用 eclipse pydev 编写:
from SpiderSimple.HtmLHelper import *import impimport sysimp.reload(sys) #sys.setdefaultencoding('utf-8') html = getHtml('http://image.baidu.com/')try: getImage(html) exit()except Exception as e: print(e)
HtmlHelper.py文件
上面的 SpiderSimple是自定义的包名
from urllib.request import urlopen,urlretrieve#正则库import re#打开网页def getHtml(url): page = urlopen(url) html = page.read() return html#用正则爬里面的图片地址 def getImage(Html): try: #reg = r'src="(.+?\.jpg)" class' #image = re.compile(reg) image = re.compile(r'<img[^>]*src[=\"\']+([^\"\']*)[\"\'][^>]*>', re.I) Html = Html.decode('utf-8') imaglist = re.findall(image,Html) x =0 for imagurl in imaglist: #将图片一个个下载到项目所在文件夹 urlretrieve(imagurl, '%s.jpg' % x) x+=1 except Exception as e: print(e)
要注意个大问题,python 默认编码的问题。
有可能报UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128),错误。这个要设置python的默认编码为utf-8.
设置最好的方式是写bat文件,
echo off
set PYTHONIOENCODING=utf8
python -u %1
然后重启电脑。
项目地址:
git@code.csdn.net:chenqiangdage/python_spider_demo.git
拿去
0 0
- python 爬虫demo
- python爬虫demo
- Python爬虫demo
- python 爬虫demo
- Python爬虫简单的demo
- python 网络爬虫(一) 简单demo
- Python爬虫原理的小demo
- python爬虫之BeautifulSoup小demo
- 爬虫demo
- python爬虫实现简单爬取淘宝商品demo
- python爬虫实现网络股票信息爬取的demo
- java 爬虫Demo
- 网页爬虫简单demo
- Android 网络爬虫demo
- Scrapy爬虫Demo
- webcollector爬虫demo
- PY爬虫Demo集合
- Java小爬虫Demo
- Sitemesh
- Linux系统\Centos没有网卡eth0配置文件怎么办?
- 引入JavaScript脚本代码到HTML文档
- java泛型基础-I
- 物联网学生科协第三届H-star现场编程比赛
- python 爬虫demo
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
- 若你早知道这些,工作后就不会混成现在的样子!
- UI 开发
- HttpClient 教程
- 李航博士:浅谈我对机器学习的理解
- Python字符串
- Gaussian LDA(1): LDA回顾以及变分EM
- SuperMap