python goose_提取数据之goose使用

1.简介

Python-goose项目是用Python重写的Goose,Goose原来是用Java写的文章提取工具。Python-goose的目标是给定任意资讯文章或者任意文章类的网页,不仅提取出文章的主体,同时提取出所有元信息以及图片等信息,支持中文网页。

Python-goose可提取的信息包括:

文章主体内容

文章主要图片

文章中嵌入的任何Youtube/Vimeo视频

元描述

元标签

2.安装

virtualenv --no-site-packages goose

cd goose#windows下

Scripts\activate#linux下使用/bin/acitvate

git clone https://github.com/grangier/python-goose.git

cd python-goose

pip install-r requirements.txt

python setup.py install

3.使用

>>> from goose import Goose

>>> url = 'http://edition.cnn.com/2012/02/22/world/europe/uk-occupy-london/index.html?hpt=ieu_c2'

>>> g = Goose()

>>> article = g.extract(url=url)

>>> article.title

u'Occupy London loses eviction fight'

>>> article.meta_description

"Occupy London protesters who have been camped outside the landmark St. Paul's Cathedral for the past four months lost their court bid to avoid eviction Wednesday in a decision made by London's Court of Appeal."

>>> article.cleaned_text[:150]

(CNN) -- Occupy London protesters who have been camped outside the landmark St. Paul's Cathedral for the past four months lost their court bid to avoi

>>> article.top_image.src

http://i2.cdn.turner.com/cnn/dam/assets/111017024308-occupy-london-st-paul-s-cathedral-story-top.jpg

对于中文文章,需要

g = Goose({'browser_user_agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.3

6','stopwords_class':StopWordsChinese})

参考:

首先,需要了解GOOSE报文的格式和数据结构,以便能够正确地解析报文并提取数据。GOOSE报文是一种通用对象操作序列(GOOSE)的通信方式,用于在现场设备之间进行通信。 GOOSE报文由多个字段组成,其中包括源MAC地址、目的MAC地址、VLAN ID、GOOSE标记等。在解析报文时,需要使用Verilog实现一个解析器,该解析器可以识别报文的各个字段,并将它们存储在FIFO中。 以下是一个简单的Verilog代码示例,可用于解析GOOSE报文并将其存储在FIFO中: ``` module goose_parser ( input clk, // 输入时钟 input reset, // 复位信号 input [7:0] rx_data, // 接收到的数据 output reg [7:0] fifo_data, // FIFO 数据 output reg fifo_valid, // FIFO 有效信号 output reg [7:0] fifo_count // FIFO 计数器 ); // 定义报文字段的位宽和偏移量 parameter MAC_ADDR_WIDTH = 48; parameter VLAN_ID_WIDTH = 12; parameter GOOSE_FLAG_WIDTH = 1; parameter MAC_ADDR_OFFSET = 0; parameter VLAN_ID_OFFSET = MAC_ADDR_WIDTH; parameter GOOSE_FLAG_OFFSET = VLAN_ID_OFFSET + VLAN_ID_WIDTH; // 定义报文字段信号 reg [MAC_ADDR_WIDTH-1:0] src_mac_addr; reg [MAC_ADDR_WIDTH-1:0] dst_mac_addr; reg [VLAN_ID_WIDTH-1:0] vlan_id; reg [GOOSE_FLAG_WIDTH-1:0] goose_flag; // 定义解析状态 reg [2:0] state; // 定义FIFO reg [7:0] fifo[255:0]; reg [7:0] fifo_head; reg [7:0] fifo_tail; reg [7:0] fifo_count; // 定义接收缓冲区 reg [7:0] rx_buf[255:0]; reg [7:0] rx_head; reg [7:0] rx_tail; reg [7:0] rx_count; // 初始化解析状态和FIFO initial begin state = 0; fifo_head = 0; fifo_tail = 0; fifo_count = 0; end // 接收缓冲区读指针 always @(posedge clk) begin if (reset) begin rx_head <= 0; end else if (rx_count > 0) begin rx_head <= (rx_head + 1) % 256; end end // 接收缓冲区写指针 always @(posedge clk) begin if (reset) begin rx_tail <= 0; end else if (rx_count < 256) begin rx_tail <= (rx_tail + 1) % 256; end end // 接收缓冲区计数器 always @(posedge clk) begin if (reset) begin rx_count <= 0; end else if (rx_count < 256 && rx_data != 8'h00) begin rx_count <= rx_count + 1; end end // 接收缓冲区写入数据 always @(posedge clk) begin if (reset) begin rx_buf[0] <= 8'h00; end else if (rx_count < 256) begin rx_buf[rx_tail] <= rx_data; end end // 解析器状态机 always @(posedge clk) begin case (state) 0: begin if (rx_count >= 14) begin src_mac_addr <= {rx_buf[1], rx_buf[2], rx_buf[3], rx_buf[4], rx_buf[5], rx_buf[6]}; dst_mac_addr <= {rx_buf[7], rx_buf[8], rx_buf[9], rx_buf[10], rx_buf[11], rx_buf[12]}; vlan_id <= {rx_buf[15], rx_buf[16]}; goose_flag <= rx_buf[17][0]; state <= 1; end end 1: begin if (rx_count >= 18) begin fifo[fifo_head] <= rx_buf[18]; fifo_head <= (fifo_head + 1) % 256; fifo_count <= fifo_count + 1; state <= 0; end end endcase end // FIFO读指针 always @(posedge clk) begin if (reset) begin fifo_tail <= 0; end else if (fifo_count > 0 && fifo_valid) begin fifo_tail <= (fifo_tail + 1) % 256; end end // FIFO计数器 always @(posedge clk) begin if (reset) begin fifo_count <= 0; end else if (fifo_count > 0 && fifo_valid) begin fifo_count <= fifo_count - 1; end end // FIFO输出信号 always @(posedge clk) begin if (reset) begin fifo_data <= 8'h00; fifo_valid <= 0; end else if (fifo_count > 0) begin fifo_data <= fifo[fifo_tail]; fifo_valid <= 1; end else begin fifo_data <= 8'h00; fifo_valid <= 0; end end endmodule ``` 在以上代码中,首先定义了报文字段的位宽和偏移量,以便可以正确地识别各个字段。然后,定义了解析状态、FIFO和接收缓冲区,以及读写指针和计数器。接下来,使用Verilog编写了一个状态机,该状态机可以将接收到的数据解析为GOOSE报文,并将GOOSE数据存储在FIFO中。最后,定义了FIFO输出信号,该信号可以将FIFO中的数据输出到其他模块中。 需要注意的是,以上代码仅供参考,需要根据实际需求进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值