使用Python和newspaper3k库进行Web抓取时不会返回数据

使用Python和newspaper3k库进行Web抓取时不会返回数据,python,web-scraping,python-newspaper,newspaper3k,Python,Web Scraping,Python Newspaper,Newspaper3k,我已经用sudo pip3 install Newspapper3k在我的Mac电脑上安装了Newspapper3kLib。我正在使用Python3。 我想返回文章对象支持的数据,即url、日期、标题、文本、摘要和关键字,但我没有得到任何数据: import newspaper from newspaper import Article #creating website for scraping cnn_paper = newspaper.build('https://www.euronew

我已经用sudo pip3 install Newspapper3k在我的Mac电脑上安装了
Newspapper3k
Lib。我正在使用Python3。 我想返回文章对象支持的数据,即url、日期、标题、文本、摘要和关键字,但我没有得到任何数据:

import newspaper
from newspaper import Article

#creating website for scraping
cnn_paper = newspaper.build('https://www.euronews.com/', memoize_articles=False)

#I have tried for https://www.euronews.com/, https://edition.cnn.com/, https://www.bbc.com/


for article in cnn_paper.articles:

    article_url = article.url #works

    news_article = Article(article_url)#works

    print("OBJECT:", news_article, '\n')#works
    print("URL:", article_url, '\n')#works
    print("DATE:", news_article.publish_date, '\n')#does not work
    print("TITLE:", news_article.title, '\n')#does not work
    print("TEXT:", news_article.text, '\n')#does not work
    print("SUMMARY:", news_article.summary, '\n')#does not work
    print("KEYWORDS:", news_article.keywords, '\n')#does not work
    print()
    input()
我得到了文章对象和URL,但其他所有内容都是“”。我在不同的网站上试过,但结果是一样的

然后我试图补充:

    news_article.download()
    news_article.parse()
    news_article.nlp()
我也尝试过设置配置,设置标题和超时,但结果是一样的

当我这样做时,每个网站我只收到16篇带有日期、标题和正文值的文章。这对我来说很奇怪,每个网站的数据量都是一样的,但95%以上的新闻文章都没有

漂亮的汤能帮我吗

有人能帮我理解问题出在哪里,为什么我会得到这么多Null/Nan/“”值,以及如何解决这个问题吗

这是lib的文档:


我建议您查看我在GitHub上发布的文档。文档中有多个提取示例和其他可能有用的技术

关于你的问题

Newspaper3K将几乎完美地解析某些网站。但是有很多网站需要检查页面的导航结构,以确定如何正确解析文章元素

例如,在页面的meta标记部分中存储了单独的文章元素,例如标题、发布日期和其他项目

下面的示例将正确解析元素。我注意到您可能需要对关键字或标记输出进行一些数据清理

import newspaper
from newspaper import Config
from newspaper import Article

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

base_url = 'https://www.marketwatch.com'
article_urls = set()
marketwatch = newspaper.build(base_url, config=config, memoize_articles=False, language='en')
for sub_article in marketwatch.articles:
article = Article(sub_article.url, config=config, memoize_articles=False, language='en')
article.download()
article.parse()
if article.url not in article_urls:
    article_urls.add(article.url)

    # The majority of the article elements are located
    # within the meta data section of the page's
    # navigational structure
    article_meta_data = article.meta_data

    published_date = {value for (key, value) in article_meta_data.items() if key == 'parsely-pub-date'}
    article_published_date = " ".join(str(x) for x in published_date)

    authors = sorted({value for (key, value) in article_meta_data.items() if key == 'parsely-author'})
    article_author = ', '.join(authors)

    title = {value for (key, value) in article_meta_data.items() if key == 'parsely-title'}
    article_title = " ".join(str(x) for x in title)

    keywords = ''.join({value for (key, value) in article_meta_data.items() if key == 'keywords'})
    keywords_list = sorted(keywords.lower().split(','))
    article_keywords = ', '.join(keywords_list)

    tags = ''.join({value for (key, value) in article_meta_data.items() if key == 'parsely-tags'})
    tag_list = sorted(tags.lower().split(','))
    article_tags = ', '.join(tag_list)

    summary = {value for (key, value) in article_meta_data.items() if key == 'description'}
    article_summary = " ".join(str(x) for x in summary)

    # the replace is used to remove newlines
    article_text = article.text.replace('\n', '')
    print(article_text)
类似于,, 除此之外,一些文章元素位于主体中,其他项目位于元标记部分中

import newspaper
from newspaper import Config
from newspaper import Article

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

base_url = 'https://www.euronews.com'
article_urls = set()
euronews = newspaper.build(base_url, config=config, memoize_articles=False, language='en')
for sub_article in euronews.articles:
   if sub_article.url not in article_urls:
     article_urls.add(sub_article.url)
     article = Article(sub_article.url, config=config, memoize_articles=False, language='en')
     article.download()
     article.parse()

     # The majority of the article elements are located
     # within the meta data section of the page's
     # navigational structure
     article_meta_data = article.meta_data
    
     published_date = {value for (key, value) in article_meta_data.items() if key == 'date.created'}
     article_published_date = " ".join(str(x) for x in published_date)
    
     article_title = article.title

     summary = {value for (key, value) in article_meta_data.items() if key == 'description'}
     article_summary = " ".join(str(x) for x in summary)

     keywords = ''.join({value for (key, value) in article_meta_data.items() if key == 'keywords'})
     keywords_list = sorted(keywords.lower().split(','))
     article_keywords = ', '.join(keywords_list).strip()

     # the replace is used to remove newlines
     article_text = article.text.replace('\n', '')

目前,我正在努力改进我的答案,所以今晚我会看这篇文章,并为你发布一个答案。你是最好的!成为最优秀的人是否值得投票;-]对不起,我没听懂你想说什么?别担心。关于报纸,NLP代码无法正常工作,因此我将研究如何为您提供文章摘要。谢谢,如果我将html从beautiful soup传递到文章,是否有更多机会获得文章值?还有什么是请求限制,我可以发布的请求量是否最大?还有,我如何提取meta_关键字和作者姓名,以及是否有方法识别新闻的语言(例如,如果我开始收集网站上的所有新闻,而网站上有英语、法语等新闻,我可以只选择英语新闻)?@taga当我询问euronews时,我注意到我通过paper.build获得了多种语言,但我相信在下一轮使用Article()时,这种情况发生了变化。在某些情况下,您可能需要使用selenium“单击”网站上的某些内容以获得所需的语言。@taga我上面的示例演示了如何提取元项。一些项目(例如关键字、作者)可能存在于元标记中,有时这些项目不存在,需要额外的代码来删除。My Github提供了许多示例(例如,beautiful soup),因此请查看该文档。@taga和关于源代码的“请求限制”。这是你必须衡量自己的东西,因为任何来源都可能在你点击它的时候中断你的连接。我通常会在代码中添加一个time.sleep片段。