日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python newspaper_第74天:Python newspaper 框架

發(fā)布時(shí)間:2024/9/27 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python newspaper_第74天:Python newspaper 框架 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

by 程序員野客

1 簡(jiǎn)介

newspaper 框架是一個(gè)主要用來提取新聞內(nèi)容及分析的 Python 爬蟲框架,更確切的說,newspaper 是一個(gè) Python 庫,但這個(gè)庫由第三方開發(fā)。

newspaper 主要具有如下幾個(gè)特點(diǎn):

比較簡(jiǎn)潔

速度較快

支持多線程

支持多語言

安裝方法:pip3 install newspaper3k

2 基本使用

2.1 查看支持語言

import newspaper

print(newspaper.languages())

2.2 獲取新聞

我們以環(huán)球網(wǎng)為例,如下所示:

import newspaper

hq_paper = newspaper.build("https://tech.huanqiu.com/", language="zh", memoize_articles=False)

默認(rèn)情況下,newspaper 緩存所有以前提取的文章,并刪除它已經(jīng)提取的任何文章,使用 memoize_articles 參數(shù)選擇退出此功能。

2.3 獲取文章 URL

>>> import newspaper

>>> hq_paper = newspaper.build("https://tech.huanqiu.com/", language="zh", memoize_articles=False)

>>> for article in hq_paper.articles:

>>> print(article.url)

http://world.huanqiu.com/gallery/9CaKrnQhXvy

http://mil.huanqiu.com/gallery/7RFBDCOiXNC

http://world.huanqiu.com/gallery/9CaKrnQhXvz

http://world.huanqiu.com/gallery/9CaKrnQhXvw

...

2.4 獲取類別

>>> import newspaper

>>> hq_paper = newspaper.build("https://tech.huanqiu.com/", language="zh", memoize_articles=False)

>>> for category in hq_paper.category_urls():

>>> print(category)

http://www.huanqiu.com

http://tech.huanqiu.com

http://smart.huanqiu.com

https://tech.huanqiu.com/

2.5 獲取品牌和描述

>>> import newspaper

>>> hq_paper = newspaper.build("https://tech.huanqiu.com/", language="zh", memoize_articles=False)

>>> print(hq_paper.brand)

>>> print(hq_paper.description)

huanqiu

環(huán)球網(wǎng)科技,不一樣的IT視角!以“成為全球科技界的一面鏡子”為出發(fā)點(diǎn),向關(guān)注國(guó)際科技類資訊的網(wǎng)民,提供國(guó)際科技資訊的傳播與服務(wù)。

2.6 下載解析

我們選取其中一篇文章為例,如下所示:

>>> import newspaper

>>> hq_paper = newspaper.build("https://tech.huanqiu.com/", language="zh", memoize_articles=False)

>>> article = hq_paper.articles[4]

# 下載

>>> article.download()

# 解析

article.parse()

# 獲取文章標(biāo)題

>>> print("title=", article.title)

# 獲取文章日期

>>> print("publish_date=", article.publish_date)

# 獲取文章作者

>>> print("author=", article.authors)

# 獲取文章頂部圖片地址

>>> print("top_iamge=", article.top_image)

# 獲取文章視頻鏈接

>>> print("movies=", article.movies)

# 獲取文章摘要

>>> print("summary=", article.summary)

# 獲取文章正文

>>> print("text=", article.text)

title= “美麗山”的美麗傳奇

publish_date= 2019-11-15 00:00:00

...

2.7 Article 類使用

from newspaper import Article

article = Article('https://money.163.com/19/1130/08/EV7HD86300258105.html')

article.download()

article.parse()

print("title=", article.title)

print("author=", article.authors)

print("publish_date=", article.publish_date)

print("top_iamge=", article.top_image)

print("movies=", article.movies)

print("text=", article.text)

print("summary=", article.summary)

2.8 解析 html

我們通過 requests 庫獲取文章 html 信息,用 newspaper 進(jìn)行解析,如下所示:

import requests

from newspaper import fulltext

html = requests.get('https://money.163.com/19/1130/08/EV7HD86300258105.html').text

print('獲取的原信息-->', html)

text = fulltext(html, language='zh')

print('解析后的信息', text)

2.9 nlp(自然語言處理)

我們看一下在 nlp 處理前后獲取一篇新聞的關(guān)鍵詞情況,如下所示:

>>> from newspaper import Article

>>> article = Article('https://money.163.com/19/1130/08/EV7HD86300258105.html')

>>> article.download()

>>> article.parse()

>>> print('處理前-->', article.keywords)

# nlp 處理

>>> article.nlp()

>>> print('處理后-->', article.keywords)

處理前--> []

處理后--> ['亞洲最大水秀項(xiàng)目成擺設(shè)', '至今拖欠百萬設(shè)計(jì)費(fèi)']

通過結(jié)果我們可以看出 newspaper 框架的 nlp 處理效果還算可以。

2.10 多任務(wù)

當(dāng)我們需要從多個(gè)渠道獲取新聞信息時(shí)可以采用多任務(wù)的方式,如下所示:

import newspaper

from newspaper import news_pool

hq_paper = newspaper.build('https://www.huanqiu.com', language="zh")

sh_paper = newspaper.build('http://news.sohu.com', language="zh")

sn_paper = newspaper.build('https://news.sina.com.cn', language="zh")

papers = [hq_paper, sh_paper, sn_paper]

# 線程數(shù)為 3 * 2 = 6

news_pool.set(papers, threads_per_source=2)

news_pool.join()

print(hq_paper.articles[0].html)

因獲取內(nèi)容較多,上述代碼執(zhí)行可能需要一段時(shí)間,我們要耐心等待。

3 詞云實(shí)現(xiàn)

下面我們來看一下如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的詞云。

需要的庫

import newspaper

# 詞頻統(tǒng)計(jì)庫

import collections

# numpy 庫

import numpy as np

# 結(jié)巴分詞

import jieba

# 詞云展示庫

import wordcloud

# 圖像處理庫

from PIL import Image

# 圖像展示庫

import matplotlib.pyplot as plt

第三方庫的安裝使用 pip install 即可,如:pip install wordcloud。

文章獲取及處理

# 獲取文章

article = newspaper.Article('https://news.sina.com.cn/o/2019-11-28/doc-iihnzahi3991780.shtml')

# 下載文章

article.download()

# 解析文章

article.parse()

# 對(duì)文章進(jìn)行 nlp 處理

article.nlp()

# nlp 處理后的文章拼接

article_words = "".join(article.keywords)

# 精確模式分詞(默認(rèn)模式)

seg_list_exact = jieba.cut(article_words, cut_all=False)

# 存儲(chǔ)分詞結(jié)果

object_list = []

# 移出的詞

rm_words = ['迎', '以來', '將']

# 迭代分詞對(duì)象

for word in seg_list_exact:

if word not in rm_words:

object_list.append(word)

# 詞頻統(tǒng)計(jì)

word_counts = collections.Counter(object_list)

# 獲取前 10 個(gè)頻率最高的詞

word_top10 = word_counts.most_common(10)

# 詞條及次數(shù)

for w, c in word_top10:

print(w, c)

生成詞云

# 詞頻展示

# 定義詞頻背景

mask = np.array(Image.open('bg.jpg'))

wc = wordcloud.WordCloud(

# 設(shè)置字體格式

font_path='C:/Windows/Fonts/simhei.ttf',

# 背景圖

mask=mask,

# 設(shè)置最大顯示的詞數(shù)

max_words=100,

# 設(shè)置字體最大值

max_font_size=120

)

# 從字典生成詞云

wc.generate_from_frequencies(word_counts)

# 從背景圖建立顏色方案

image_colors = wordcloud.ImageColorGenerator(mask)

# 顯示詞云

plt.imshow(wc)

# 關(guān)閉坐標(biāo)軸

plt.axis('off')

plt.savefig('wc.jpg')

# 顯示圖像

plt.show()

效果如圖所示:

總結(jié)

本文為大家介紹了 Python 爬蟲框架 newspaper,讓大家能夠?qū)?newspaper 有個(gè)基本了解以及能夠上手使用。在使用的過程中,我們會(huì)發(fā)現(xiàn) newspaper 框架還存在一些 bug,因此,我們?cè)趯?shí)際工作中需要綜合考慮、謹(jǐn)慎使用。

參考:

關(guān)注公眾號(hào):python技術(shù),回復(fù)"python"一起學(xué)習(xí)交流

總結(jié)

以上是生活随笔為你收集整理的python newspaper_第74天:Python newspaper 框架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。