Python scrapy框架爬虫demo

encoding=utf8

import scrapy
import time
from scrapyLuntan.items import ScrapyluntanItem
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
lis = []
class LunTan(scrapy.Spider):
# 这个爬虫的识别名称,必须是唯一的,在不同的爬虫必须定义不同的名字
name='luntan'
# 是搜索的域名范围,也就是爬虫的约束区域,
# 规定爬虫只爬取这个域名下的网页,不存在的URL会被忽略。
allowd_domains = ['http://dzh2.mop.com/']
#爬取的URL元祖 / 列表。爬虫从这里开始抓取数据,
# 所以,第一次下载的数据将会从这些urls开始。其他子URL将会从这些起始URL中继承性生成。
start_urls = ('http://www.mop.com/',)
#解析的方法,每个初始URL完成下载后将被调用,
# 调用的时候传入从每一个URL传回的Response对象来作为唯一参数
def parse(self, response):
li_list = response.xpath("//li[@class='mop-item-a']")
print len(li_list)
for i in li_list:
a_href = i.xpath("./a/@href")[0].extract()
a_href = a_href.replace('http://dzh2.mop.com/dzh_index.html#rlink=','')
print a_href
# 发送新的url请求加入待爬队列,并调用回调函数 self.parse
yield scrapy.Request(a_href, meta={'a_href': a_href},callback=self.res_detail)

def res_detail(self, response):
    item = ScrapyluntanItem()
    detail_url = response.meta['a_href']
    print response.meta['a_href']
    if 'http://dzh2.mop.com/' in detail_url:
        text = response.xpath("//div[@class='post-date fl mr15']/span/text()")[0].extract()
    else:
        text = response.xpath("//div[@class='mr20 inlineBlock']/span/text()")[0].extract()

    text = text.strip()
    print text
    if '年' in text:
        print 88888
        text = text.replace('年','-')
        text = text.replace('月','-')
        text = text.replace('日','')

    timeArray = time.strptime(text,"%Y-%m-%d %H:%M:%S")
    item['time'] = int(time.mktime(timeArray))
    open('./time.txt','a+').write(str(item['time'])+"\r\n")
    yield item

    # print timeStamp
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • scrapy学习笔记(有示例版) 我的博客 scrapy学习笔记1.使用scrapy1.1创建工程1.2创建爬虫模...
    陈思煜阅读 12,970评论 4 46
  • Scrapy,Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化...
    Evtion阅读 6,144评论 12 18
  • 可以看我的博客 lmwen.top 或者订阅我的公众号 简介有稍微接触python的人就会知道,python中...
    ayuLiao阅读 3,363评论 1 5
  • 请点击蓝字淑女蔷薇免费关注!感恩感谢! 今天正月初七,是“人日”和“立春”同一天,可谓双喜临门! 传说人类始祖女娲...
    徐淑英柔情婉淑女蔷薇阅读 436评论 2 1
  • 大家好,我是新手宝妈,刚刚才找到这个平台,很高兴认识你,大家.我没啥说,只想和你们大家,随便的拉拉家常,聊一聊,人...
    yuhuashi阅读 1,157评论 0 1

友情链接更多精彩内容