<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何使用PYTHON抓取新聞文章

          共 2200字,需瀏覽 5分鐘

           ·

          2020-09-12 09:41

          r

          在本文中,我們將討論如何使用Python抓取新聞報(bào)道。這可以使用方便的報(bào)紙包裝來完成

          Python?newspaper?包簡(jiǎn)介

          可以使用pip安裝newspaper?包:

          pip install newspaper

          安裝完成后,即可開始。?newspaper可以通過從給定的URL上抓取一篇文章,或者通過找到網(wǎng)頁上其他新聞的鏈接來工作。讓我們從處理一篇文章開始。首先,我們需要導(dǎo)入Article類。接下來,我們使用此類將內(nèi)容從URL下載到我們的新聞文章。然后,我們使用parse方法解析HTML。最后,我們可以使用.text打印文章的文本

          爬一篇文章


          from newspaper import Article url = "https://www.bloomberg.com/news/articles/2020-08-01/apple-buys-startup-to-turn-iphones-into-payment-terminals?srnd=premium" # download and parse articlearticle = Article(url)article.download()article.parse() # print article textprint(article.text)

          還可以獲取有關(guān)文章的其他信息,例如指向帖子中嵌入的圖像或視頻的鏈接。

          # get list of image linksarticle.images # get list of videos - empty in this casearticle.movies

          下載網(wǎng)頁上鏈接的所有文章

          現(xiàn)在,讓我們看看如何將所有新聞文章鏈接到網(wǎng)頁上。我們將使用如下的news.build方法來實(shí)現(xiàn)然后,我們可以使用article_urls方法提取文章URL?

          import newspaper site = newspaper.build("https://news.ycombinator.com/")   # get list of article URLssite.article_urls()

          使用上面的對(duì)象,我們還可以獲得每個(gè)文章的內(nèi)容。這里,所有article對(duì)象都存儲(chǔ)在list.site.articles中例如,讓我們獲取第一篇文章的內(nèi)容。

          site_article = site.articles[0] site_article.download()site_article.parse() print(site_article.text)

          現(xiàn)在,讓我們修改代碼以獲得前十名文章:

          top_articles = []for index in range(10):    article = site.articles[index]    article.download()    article.parse()    top_articles.append(article)

          警告!

          使用時(shí),一個(gè)重要的說明newspaper的是,如果你運(yùn)行newspaper.build用相同的URL多次,
          包將緩存,然后刪除已經(jīng)刮了文章。例如,在下面的代碼中,我們連續(xù)兩次運(yùn)行Newspaper.build并獲得不同的結(jié)果。第二次運(yùn)行它時(shí),代碼僅返回新添加的鏈接。

          site = newspaper.build("https://news.ycombinator.com/")     print(len(site.articles)) site = newspaper.build("https://news.ycombinator.com/")     print(len(site.articles))

          可以通過在函數(shù)調(diào)用中添加一個(gè)額外的參數(shù)來進(jìn)行調(diào)整,如下所示:

          site = newspaper.build("https://news.ycombinator.com/", memoize_articles=False)

          如何獲得文章摘要

          newspaper包也支持一些NLP功能。您可以通過調(diào)用nlp方法進(jìn)行檢查。

          article = top_articles[3] article.nlp()

          現(xiàn)在,讓我們使用summary方法。這將嘗試返回文章摘要。

          article.summary()

          您還可以從文章中獲取關(guān)鍵字列表。

          article.keywords

          如何獲得最熱門的Google關(guān)鍵字

          報(bào)紙還有其他一些很酷的功能。例如,我們可以使用hot方法輕松使用它在Google上吸引最熱門的搜索

          newspaper.hot()

          該軟件包還可以返回受歡迎的URL列表,如下所示。

          newspaper.popular_urls()


          推薦閱讀



          送書


          瀏覽 76
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品无码专区 | 青青草在线视频黄色 | 久久天天舔 | 精品国产一 | 国产精品自产拍高潮在线观看 |