【问题标题】:newsletter3k, find author name in visible text after first "by" wordnewsletter3k,在第一个“by”字之后的可见文本中查找作者姓名
【发布时间】:2021-02-12 05:58:49
【问题描述】:

Newsletter3K 是一个很好的用于新闻内容提取的 python 库。它大部分效果很好 .我想在可见文本中的第一个“by”字之后提取名称。这是我的代码,效果不好,请高人帮忙:

import re
from newspaper import Config
from newspaper import Article
from newspaper import ArticleException
from bs4 import BeautifulSoup
from bs4.element import Comment
import urllib.request
USER_AGENT = 'Mozilla/5.0 (Macintosh;Intel Mac OS X 10.15; rv:78.0)Gecko/20100101   Firefox/78.0'
config = Config()
config.browser_user_agent = USER_AGENT
config.request_timeout = 10 
html1='https://saugeentimes.com/new-perspectives-a-senior-moment-food-glorious-food-part-2/'
article = Article(html1.strip(), config=config)
article.download()
article.parse()
soup = BeautifulSoup(article)
## I want to take only visible text
[s.extract() for s in soup(['style', 'script', '[document]', 'head', 'title'])]
visible_text = soup.getText()
for line in visible_text:
    # Capture one-or-more words after first (By or by) the initial match
    match = re.search(r'By (\S+)', line)

    # Did we find a match?
    if match:
        # Yes, process it to print 
        By = match.group(1)
        print('By {}'.format(By))`

【问题讨论】:

标签: beautifulsoup extract word visible newspaper3k


【解决方案1】:

这不是一个全面的答案,但您可以从中构建。添加其他源时,您将需要扩展此代码。就像我之前所说的,我的Newspaper3k overview document 有很多提取示例,所以请仔细阅读。

在使用 newspaper3k 尝试这些提取方法之后,正则表达式应该是最后的努力:

  • article.authors
  • 元标签
  • json
from newspaper import Config
from newspaper import Article
from newspaper.utils import BeautifulSoup

USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'

config = Config()
config.browser_user_agent = USER_AGENT
config.request_timeout = 10

urls = ['https://saugeentimes.com/new-perspectives-a-senior-moment-food-glorious-food-part-2',
        'https://www.macleans.ca/education/what-college-students-in-canada-can-expect-during-covid',
        'https://www.cnn.com/2021/02/12/asia/india-glacier-raini-village-chipko-intl-hnk/index.html',
        'https://www.latimes.com/california/story/2021-02-13/wildfire-santa-cruz-boulder-creek-residents-fear-water'
        '-quality',
        'https://foxbaltimore.com/news/local/maryland-lawmakers-move-ahead-with-first-tax-on-internet-ads-02-13-2021']

for url in urls:
    try:
        article = Article(url, config=config)
        article.download()
        article.parse()
        author = article.authors
        if author:
            print(author)
        elif not author:
            soup = BeautifulSoup(article.html, 'html.parser')
            author_tag = soup.find(True, {'class': ['td-post-author-name', 'byline']}).find(['a', 'span'])
            if author_tag:
                print(author_tag.get_text().replace('By', '').strip())
            else:
                print('no author found')
    except AttributeError as e:
        pass

【讨论】:

  • 现在,我对使用 newsletter3k 作为网络文章抓取的完整解决方案存有疑问:标题、URL、作者、图像、文本、发布日期和摘要。各位高手,有没有比paper3k更好的工具可以试试?
  • newsletter3k 并不完美,但它是一个不错的 python 模块。我已经了解了模块的参数,并且知道我需要查看目标网站的来源以确定哪些收获方法将起作用。使用美丽的汤、硒或刮痧时,您必须做同样的事情。我的 newsletter3k 概述文档旨在告知其他人该模块针对特定站点的用法。该文档为任何人提供了有关如何高效使用库的基础知识。
  • 谢谢,我把它当作没有答案。所以报纸仍然是一种选择。
  • 不是否定,而是可能有限制。
  • 我的意思是,时事通讯还有待解决。
猜你喜欢
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
相关资源
最近更新 更多