online panel在广告曝光日志中的特征进一步提取
来源:互联网 发布:托拜厄斯哈里斯数据 编辑:程序博客网 时间:2024/04/28 04:37
Scrapy解析url视频基本信息(不爬取)
好久没写过博客了,近来感觉还是有必要mark下工作的内容。从今天开始吧。
现手上有一批广告曝光日志,与online panel(在线样本库)(交集的量还是很大的)。由于online panel的用户标识id很少,因此想基于现有的panel,从曝光日志中获取一部分特征,来构建个分类器,以期评判那些不在online panel的用户的ta(target audience)属性。
然而,现有的数据携带的特征较少,只有用户行为的ip、访问url、来源url、点击或者曝光等。基于现有特征的分类器的预测ta类别效果不佳。因此想到,通过用户访问的url,去对应网站获得一些基本信息来强化特征。本文就是基于这种背景去调研的。
提供url解析的工具不少,例如htmlpaser、beatiful soup、lxml等。但为了以后业务的拓展(可能进行视频基本信息爬取),最终采用了scrapy。
scrapy是一个开源爬虫框架。这里不在赘述具体的介绍和使用,有需要的还是老老实实去看官方文档吧。不是很难,很快就能上手。
事实上,我这个任务不涉及到爬取功能,主要操作是爬虫的start_urls,因为我们已经有url了,只做这些url的基本信息解析。
手上的广告曝光日志主要是一些视频网站的url,有用的特征很多,但容易提取出来的特征主要就是视频标题,视频标签?,视频类别以及描述。
理解scrapy后,实现挺简单的,先贴出解析youku的视频(仅限于v.youku.com)的代码实现吧。
首先是items.py:
import scrapyfrom scrapy.item import Item,Fieldclass YoukuItem(Item): url = Field() title = Field() crumbs = Field() description = Field() tags = Field()
然后是piplelines.py:
# -*- coding: utf-8 -*-# Define your item pipelines here## Don't forget to add your pipeline to the ITEM_PIPELINES setting# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.htmlimport MySQLdbimport MySQLdb.cursorsfrom twisted.enterprise import adbapifrom scrapy import logclass YoukuPipeline(object): def __init__(self): self.dbpool = adbapi.ConnectionPool('MySQLdb', db = 'medias', user = 'root', passwd = 'root', cursorclass = MySQLdb.cursors.DictCursor, charset = 'utf8', use_unicode = True ) def process_item(self, item, spider): if spider.name in ['youkucrawl']: query = self.dbpool.runInteraction(self._conditional_insert, item) query.addErrback(self.handle_error) else: print item return item def _conditional_insert(self, tx, item): if item.get('url'): tx.execute('replace into youku values (%s, %s, %s, %s, %s)', (item['url'], item['title'], item['crumbs'], item['description'], item['tags'])) def handle_error(self, e): log.err(e)
最后是spider的实现:
#!/usr/bin/env python#coding=utf8import scrapy, osimport tracebackfrom scrapy.spider import Spiderfrom myspider.items import YoukuItemclass YoukuSpider(Spider): def __init__(self, url = None, *args, **kwargs): super(YoukuSpider, self).__init__(*args, **kwargs) if url != None: self.start_urls = [url] else: youkufilename = 'youku.txt' if os.path.exists(youkufilename): youkudata = open(youkufilename,'r') while 1: lines = youkudata.readlines(100000) if not lines: break for line in lines: line = line.strip() if line.startswith("http://v.youku.com"): self.start_urls.append(line) #start_urls = ["http://v.youku.com/v_show/id_XOTIyNjkwODk2.html?ev=1&from=y1.1-2.10001-0.1-1"] allowed_domains = ["v.youku.com"] name = 'youkucrawl' def parse(self, response): try: self.log('%s comes in!' % response.url) item = YoukuItem() item['url'] = response.url item['title'] = ','.join(response.xpath('//h1[@class="title"]').xpath('string(.)').extract()).replace('\n','').replace('\t',' ').strip() item['crumbs'] = ",".join(response.xpath('//div[@class="crumbs"]').xpath('string(.)').extract()).replace('\n','').replace('\t',' ').strip() item['tags'] = ','.join(response.xpath('//span[@class="tag"]').xpath('string(.)').extract()).replace('\n','').replace('\t',' ').strip() item['description'] = ','.join(response.css('meta[name="description"]::attr(content)').extract()).replace('\n','').replace('\t',' ').strip() print response.url + ':' + item['title'].encode('utf8') return item except Exception,e: print traceback.format_exc()
这里是配置文件:
# -*- coding: utf-8 -*-# Scrapy settings for myspider project## For simplicity, this file contains only the most important settings by# default. All the other settings are documented here:## http://doc.scrapy.org/en/latest/topics/settings.html#BOT_NAME = 'myspider'SPIDER_MODULES = ['myspider.spiders']NEWSPIDER_MODULE = 'myspider.spiders'ITEM_PIPELINES = {'myspider.pipelines.YoukuPipeline':1000,}# Crawl responsibly by identifying yourself (and your website) on the user-agent#USER_AGENT = 'myspider (+http://www.yourdomain.com)'
这里仅提供了一个简单的版本,实现对youku的视频基本信息获取,事实上可以做一个更简单的通用版本,仅获得视频title、keywords和description,从html的meta中获得。
该程序可通过以下命令测试单个url的结果:
scrapy crawl youkucrawl -a url=http://v.youku.com/v_show/id_XOTI0MjI2MDY0.html?f=23465260&ev=1
批量处理可在项目主目录下新建个youku.txt测试结果,生成结果存取在mysql中。运行命令为:
scrapy crawl youkucrawl
- online panel在广告曝光日志中的特征进一步提取
- 展示广告-广告特征提取
- 广告特征提取
- HOG特征及在opencv中的提取
- S变换在特征提取中的使用
- 展示广告-网页特征提取
- 展示广告-用户特征提取
- kaldi中的特征提取
- 模式识别中的特征提取
- 關於人臉檢測中的Haar特征提取
- 浅谈模式识别中的特征提取
- 语音信号中的特征提取
- 浅谈模式识别中的特征提取
- 语音信号中的特征提取
- 浅谈模式识别中的特征提取
- 广告点击率预估中的特征选择
- 广告点击率(ctr)预估中的特征选择
- 广告点击率预估中的特征选择
- JAVAMail初解
- float double的内存表示及比较大小的方法
- mysql约束1
- listFiles(filenameFilter)方法的源码
- 英特尔欲斥资100多亿美元并购Altera?
- online panel在广告曝光日志中的特征进一步提取
- 抽象类和接口的区别
- vs2008中打了SP1补丁包,给工具栏按钮单独贴位图(注意:是“单独”,也就是有多张图)
- Java并发编程:synchronized
- 数据库级别的优化
- [Material Design] 教你做一个Material风格、动画的按钮(MaterialButton)
- mysql分组查询聚合函数
- MPAndroidChart开源图表库(三)之柱形图
- 关于C++/JAVA类中的static成员和对象成员的说法正确的是: