小白写爬虫之第一天
2015-09-09 17:24
441 查看
本人python小白一枚,只会简单的linux命令而已,本着闲着也是闲着,生命在于折腾的原则,向spider发起冲锋,
第一步肯定是度娘了,发现如下:
Python中urllib和urllib2库的用法
正则表达式
爬虫框架 Scrapy
高级功能
让我用urlib手动玩,有点搞不懂,毕竟是python小白,
那就选 爬虫框架 Scrapy了,废话少说,
系统是ubuntu14,04
不免又百度 怎么安装Scrapy,这个好办:http://jingyan.baidu.com/article/f3ad7d0f129c7809c2345b56.html
装完了怎么用呢?
看下面这个教程 http://scrapy-chs.readthedocs.org/zh_CN/latest/intro/tutorial.html
看完开始写参考一下几篇文章:
参考这篇微博:/article/8003127.html
还有这篇:/article/1550136.html
还有这篇:/article/3483892.html
加上这篇:/article/3497805.html
照着教程搞一发:
目标:
美女吧的众美女贴图 当然你可以去什么黑丝吧 爆照吧 等等等等 哇 一大波福利啊~
折腾了一天就搞会了一个提取url,我也是醉了
说一下:
新建工程啥玩意的请看上面的教程 http://scrapy-chs.readthedocs.org/zh_CN/latest/intro/tutorial.html
用scrapy主要有一下几个文件
items.py
#
-*- coding: utf-8 -*-
#
Define here the models for your scraped items
#
#
See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html
import
scrapy
from
scrapy.item import Item,Field
class
TiebaItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
img_url = scrapy.Field()
pass
自定义的spider文件
tiebaSpider.py
#-*- coding:utf-8 -*-
#coding: UTF-8
import scrapy
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
#from TieBaSpider.items import TiebaItem
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
#写一个蜘蛛 继承自scrapy.spider
class TieBaSpider(scrapy.spiders.Spider):
name = "tieba" #名字 一定要注意
download_delay = 1 #延时
allowed_domains = ["tieba.baidu.com"] #域名空间 超出域名就不解析了
#起始网址
start_urls = [
"http://tieba.baidu.com/p/4012183664",
"http://tieba.baidu.com/p/4029206429",
"http://tieba.baidu.com/p/4031033974",
"http://tieba.baidu.com/p/4020708988",
]
#因为小白还没整出来递归调用 不能自动解析链接进行递归爬行,所以根据贴吧的url规律 进行尾数累加组成url
url_1 = "http://tieba.baidu.com/p/"
start = 4029206429
for i in range(300): #暂定循环300次
next_url = url_1 + str(start + i ) #拼接url
print next_url #显示url
start_urls.append(next_url) #添加到起始url中
f = open('/home/linux/python/tieba.html','wb') #打开文件
def parse(self, response): #这个函数必须实现 用于内容提取
hxs=HtmlXPathSelector(response) #
sites = hxs.select('//div/div/cc/div/img/@src').extract() #用firebug工具 发现到图片的xpath规律,然后提取图片的url
items=[]
for site in sites: #将图片的url写到 html文件中
self.f.write('<img src=') #用img标签来显示图片
self.f.write(site)
#程序运行完成后可以用浏览器打开tieba.html查看图片
self.f.write('>')
self.f.write('\r\n')
return items
#搞了一天才整出来一个下载图片的url,实在是才疏学浅,基础薄弱,但是每天能有进步就ok
#明天希望能完成一个自动解析url的功能出来 加油!
#今天到此为止
一会公司要周年庆 吃饭去了!
#晚上回去爬某丝吧的图片去~
第一步肯定是度娘了,发现如下:
Python中urllib和urllib2库的用法
正则表达式
爬虫框架 Scrapy
高级功能
让我用urlib手动玩,有点搞不懂,毕竟是python小白,
那就选 爬虫框架 Scrapy了,废话少说,
系统是ubuntu14,04
不免又百度 怎么安装Scrapy,这个好办:http://jingyan.baidu.com/article/f3ad7d0f129c7809c2345b56.html
装完了怎么用呢?
看下面这个教程 http://scrapy-chs.readthedocs.org/zh_CN/latest/intro/tutorial.html
看完开始写参考一下几篇文章:
参考这篇微博:/article/8003127.html
还有这篇:/article/1550136.html
还有这篇:/article/3483892.html
加上这篇:/article/3497805.html
照着教程搞一发:
目标:
美女吧的众美女贴图 当然你可以去什么黑丝吧 爆照吧 等等等等 哇 一大波福利啊~
折腾了一天就搞会了一个提取url,我也是醉了
说一下:
新建工程啥玩意的请看上面的教程 http://scrapy-chs.readthedocs.org/zh_CN/latest/intro/tutorial.html
用scrapy主要有一下几个文件
items.py
#
-*- coding: utf-8 -*-
#
Define here the models for your scraped items
#
#
See documentation in:
# http://doc.scrapy.org/en/latest/topics/items.html
import
scrapy
from
scrapy.item import Item,Field
class
TiebaItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
img_url = scrapy.Field()
pass
自定义的spider文件
tiebaSpider.py
#-*- coding:utf-8 -*-
#coding: UTF-8
import scrapy
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
#from TieBaSpider.items import TiebaItem
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
#写一个蜘蛛 继承自scrapy.spider
class TieBaSpider(scrapy.spiders.Spider):
name = "tieba" #名字 一定要注意
download_delay = 1 #延时
allowed_domains = ["tieba.baidu.com"] #域名空间 超出域名就不解析了
#起始网址
start_urls = [
"http://tieba.baidu.com/p/4012183664",
"http://tieba.baidu.com/p/4029206429",
"http://tieba.baidu.com/p/4031033974",
"http://tieba.baidu.com/p/4020708988",
]
#因为小白还没整出来递归调用 不能自动解析链接进行递归爬行,所以根据贴吧的url规律 进行尾数累加组成url
url_1 = "http://tieba.baidu.com/p/"
start = 4029206429
for i in range(300): #暂定循环300次
next_url = url_1 + str(start + i ) #拼接url
print next_url #显示url
start_urls.append(next_url) #添加到起始url中
f = open('/home/linux/python/tieba.html','wb') #打开文件
def parse(self, response): #这个函数必须实现 用于内容提取
hxs=HtmlXPathSelector(response) #
sites = hxs.select('//div/div/cc/div/img/@src').extract() #用firebug工具 发现到图片的xpath规律,然后提取图片的url
items=[]
for site in sites: #将图片的url写到 html文件中
self.f.write('<img src=') #用img标签来显示图片
self.f.write(site)
#程序运行完成后可以用浏览器打开tieba.html查看图片
self.f.write('>')
self.f.write('\r\n')
return items
#搞了一天才整出来一个下载图片的url,实在是才疏学浅,基础薄弱,但是每天能有进步就ok
#明天希望能完成一个自动解析url的功能出来 加油!
#今天到此为止
一会公司要周年庆 吃饭去了!
#晚上回去爬某丝吧的图片去~
相关文章推荐
- boot.img提取
- Unity3D开发之Unity跟IOS交互过程
- zookeeper启动没发现错误,但是是失败的
- 题目:求s=a+aa+aaa+aaaa+aa...a的值
- phoneGap+iOS环境搭建
- 0909 编译原理1
- ACM学习历程—FZU2191完美的数字(数学)
- 0909编译原理
- 0909有关编译原理的解释
- 0909 编译原理第1次上机作业
- JConsole & JVisualVM远程监视Websphere服务器JVM的配置方法
- 内存管理
- java中的值传递和引用传递
- Notice: Undefined index: ... 问题的解决方案
- Qt 信号槽机制与事件机制四 提升篇
- sqlserver 与mysql的小区别
- 一维数组,冒泡排序(Day-3)
- UC/OS II事件管理(2)之信号量管理
- domReady方法
- Html5知识精粹纪录