关键词提取。pynlpir库实现关键词提取。

# coding:utf-8

import sys
import importlib
importlib.reload(sys)

import pynlpir

pynlpir.open()
s = '怎么才能把电脑里的垃圾文件删除'

key_words = pynlpir.get_key_words(s, weighted=True)
for key_word in key_words:
print(key_word[0], 't', key_word[1])

pynlpir.close()

百度接口:https://www.baidu.com/s?wd=机器学习 数据挖掘 信息检索

安装scrapy pip install scrapy。创建scrapy工程 scrapy startproject baidu_search。做抓取器,创建baidu_search/baidu_search/spiders/baidu_search.py文件。

# coding:utf-8

import sys
import importlib
importlib.reload(sys)

import scrapy

class BaiduSearchSpider(scrapy.Spider):
name = "baidu_search"
allowed_domains = ["baidu.com"]
start_urls = [
"https://www.baidu.com/s?wd=电脑 垃圾 文件 删除"
]

def parse(self, response):
filename = "result.html"
with open(filename, 'wb') as f:
f.write(response.body)

修改settings.py文件,ROBOTSTXT_OBEY = False,USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' ,DOWNLOAD_TIMEOUT = 5 ,

进入baidu_search/baidu_search/目录,scrapy crawl baidu_search 。生成result.html,正确抓取网页。

语料提取。搜索结果只是索引。真正内容需进入链接。分析抓取结果,链接嵌在class=c-container Div h3 a标签 href属性。url添加到抓取队列抓取。提取正文,去掉标签,保存摘要。提取url时,提取标题和摘要,scrapy.Request meta传递到处理函数parse_url,抓取完成后能接到这两个值,提取content。完整数据:url、title、abstract、content。

# coding:utf-8

import sys
import importlib
importlib.reload(sys)

import scrapy
from scrapy.utils.markup import remove_tags

class BaiduSearchSpider(scrapy.Spider):
name = "baidu_search"
allowed_domains = ["baidu.com"]
start_urls = [
"https://www.baidu.com/s?wd=电脑 垃圾 文件 删除"
]

def parse(self, response):
# filename = "result.html"
# with open(filename, 'wb') as f:
# f.write(response.body)
hrefs = response.selector.xpath('//div[contains(@class, "c-container")]/h3/a/@href').extract()
# for href in hrefs:
# print(href)
# yield scrapy.Request(href, callback=self.parse_url)
containers = response.selector.xpath('//div[contains(@class, "c-container")]')
for container in containers:
href = container.xpath('h3/a/@href').extract()[0]
title = remove_tags(container.xpath('h3/a').extract()[0])
c_abstract = container.xpath('div/div/div[contains(@class, "c-abstract")]').extract()
abstract = ""
if len(c_abstract) > 0:
abstract = remove_tags(c_abstract[0])
request = scrapy.Request(href, callback=self.parse_url)
request.meta['title'] = title
request.meta['abstract'] = abstract
yield request

def parse_url(self, response):
print(len(response.body))
print("url:", response.url)
print("title:", response.meta['title'])
print("abstract:", response.meta['abstract'])
content = remove_tags(response.selector.xpath('//body').extract()[0])
print("content_len:", len(content))

参考资料:

《Python 自然语言处理》

http://www.shareditor.com/blogshow/?blogId=43

http://www.shareditor.com/blogshow?blogId=76

欢迎推荐上海机器学习工作机会,我的微信:qingxingfengzi

学习笔记CB005:关键词、语料提取的更多相关文章

  1. IOS学习笔记之关键词@dynamic

    IOS学习笔记之关键词@dynamic @dynamic这个关键词,通常是用不到的. 它与@synthesize的区别在于: 使用@synthesize编译器会确实的产生getter和setter方法 ...

  2. ArcGIS案例学习笔记2_1_山顶点提取最大值提取

    ArcGIS案例学习笔记2_1_山顶点提取最大值提取 计划时间:第二天上午 目的:最大值提取 教程:Pdf page=343 数据:chap8/ex5/dem.tif 背景知识:等高线种类 基本等高线 ...

  3. GIS案例学习笔记-明暗等高线提取地理模型构建

    GIS案例学习笔记-明暗等高线提取地理模型构建 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:针对数字高程模型,通过地形分析,建立明暗等高线提取模型,生成具有 ...

  4. GIS案例学习笔记-水文分析河网提取地理建模

    GIS案例学习笔记-水文分析河网提取地理建模 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:针对数字高程模型,通过水文分析,提取河网 操作时间:25分钟 数据 ...

  5. Python学习笔记(2) Python提取《釜山行》人物关系

    参考:http://www.jianshu.com/p/3bd06f8816d7 项目原理:   实验基于简单共现关系,编写 Python 代码从纯文本中提取出人物关系网络,并用Gephi 将生成的网 ...

  6. 【视频编解码·学习笔记】11. 提取SPS信息程序

    一.准备工作: 回到之前SimpleH264Analyzer程序,找到SPS信息,并对其做解析 调整项目目录结构: 修改Global.h文件中代码,添加新数据类型UINT16,之前编写的工程中,UIN ...

  7. ArcGIS案例学习笔记2_2_模型构建器和山顶点提取批处理

    ArcGIS案例学习笔记2_2_模型构建器和山顶点提取批处理 计划时间:第二天下午 背景:数据量大,工程大 目的:自动化,批处理,定制业务流程,不写程序 教程:Pdf/343 数据:chap8/ex5 ...

  8. 【视频编解码·学习笔记】13. 提取PPS信息程序

    PPS结构解析 与之前解析SPS方式类似 一.定义PPS类: 在3.NAL Unit目录下,新建PicParamSet.cpp和PicParamSet.h,在这两个文件中写入类的定义和函数实现. 类定 ...

  9. swift学习笔记5——其它部分(自动引用计数、错误处理、泛型...)

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

随机推荐

  1. C# Global.asax.cs 定时任务

    定时执行更新Redis缓存操作 protected void Application_Start(object sender, EventArgs e) { Timer timer = new Tim ...

  2. 用Git将本地项目推送到github

    [博客园cnblogs笔者m-yb原创,转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708] http ...

  3. eclipse起不起来web项目

    eclipse 启动java web项目tomcat无报错,但是项目没有启动成功,可能存在以下原因 1.Maven Dependecies 可能不存在 解决:点击Add将Maven Dependeci ...

  4. ATOM:亮瞎程序员双眼的编辑器插件横空出世

    Atom 编辑器有一个叫 activate-power-mode,啥玩意?看下面的动图吧,非常有 Power ,有木有!!!自带眩晕特技,有木有!

  5. IDEA新建项目时,没有Spring Initializr选项

    换了台新电脑,然后重新安装了Intellij IDEA,创建spring boot项目的时候找不到Spring Initializr选项了. 然后百度了下,发现有前辈做出了回答,就复制存到了自己随笔里 ...

  6. pytest自动化3:fixture之conftest.py实现setup

    出处:https://www.cnblogs.com/yoyoketang/p/9390073.html 前言: 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作 ...

  7. Linux 系统运行命令 > 查看系统信息

    查看系统运行状态 一 . 查看硬件信息 - 1. cpu信息(可以通过find,whereis,locate查出路径) #cat /proc/cpuinfo 2 . 内存信息:meminfo(可以用c ...

  8. Dangerous well

    Firsttime to develop games throuth Unity3d, such a great platform! You can build your games more qui ...

  9. 2018-2019-1 20165318 20165326 实验五 通讯协议设计.md

    目录 实验内容 问题及解决 参考资料 实验内容 任务一 在Ubuntu中完成作业 openSSL OpenSSL是一个SSL协议的开源实现,采用C语言作为开发语言,具备了跨平台的能力,支持Unix/L ...

  10. 高性能JavaScript(1)

    ---------------------------------------------------------------------------------------------------- ...