【发布时间】: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))`
【问题讨论】:
-
您是想让此代码仅适用于 saugeentimes.com,还是打算使用相同的代码查询多个来源?
-
是的,我想查询多个类似于 saugeentimes.com 的来源。
-
感谢您的信息。请在您的问题中提供其他来源。
-
在以下网页中,作者姓名出现在第一个可见的“by”字之后:1.thenelsondaily.com/… 2.thenelsondaily.com/… 3.macleans.ca/education/… 4.cnn.com/2020/10/09/business/edinburgh-woollen-mill-job-cuts/…
-
作者姓名(在某些网络文章中),介于标题和日期之间
标签: beautifulsoup extract word visible newspaper3k