简体   繁体   English

如何使用Python报纸库?

[英]How to use Python newspaper library?

I'm trying to make web parser and saved it. 我正在尝试制作网络解析器并保存它。 I had found the newspaper library. 我找到了报纸图书馆。 I'm using Eclipse. 我正在使用Eclipse。 But I couldn't get good result. 但我无法取得好成绩。 Please help me. 请帮我。

import newspaper

cnn_paper = newspaper.build('http://cnn.com')

for article in cnn_paper.articles:
    print(article.url)

This is a error message: 这是一条错误消息:

Traceback (most recent call last):
  File "D:\workspace2\JesElaSearchSys\NespaperScraper_01.py", line 2, in <module>
    import newspaper
  File "C:\Python27\lib\site-packages\newspaper3k-0.1.5-py2.7.egg\newspaper\__init__.py", line 10, i
n <module>
    from .article import Article, ArticleException
  File "C:\Python27\lib\site-packages\newspaper3k-0.1.5-py2.7.egg\newspaper\article.py", line 12, in
 <module>
    from . import images
  File "C:\Python27\lib\site-packages\newspaper3k-0.1.5-py2.7.egg\newspaper\images.py", line 15, in 
<module>
    import urllib.request
ImportError: No module named request

There is nothing wrong with your code. 您的代码没有任何问题。 You will need to install or locate the newspaper and request libraries. 您需要安装或找到newspaperrequest库。 It seems newspaper / request library is missing on your development machine. 您的开发计算机上似乎缺少newspaper / request库。

$ pip install newspaper
$ pip install urllib3

Command above will help you to install it. 上面的命令将帮助您安装它。

Though there is already an answer, the newspaper library for Python2 is deprecated and should be avoided. 虽然已经有了答案,但Python2的报纸库已被弃用,应予以避免。

Newspaper has been deprecated for Python2 and it's a buggy version according to their official documentation . 报纸已被弃用于Python2,根据其官方文档 ,它是一个有缺陷的版本。 You should switch to Python3 version. 你应该切换到Python3版本。

Installation on Python 3: 在Python 3上安装:

pip3 install newspaper3k

Newspaper is a Python3 library! 报纸是一个Python3库! Or, view our deprecated and buggy Python2 branch. 或者,查看我们已弃用且有缺陷的Python2分支。 Run : pip3 install newspaper3k and NOT pip3 install newspaper. 运行:pip3安装newspaper3k而不是pip3安装报纸。 On python3 you must install newspaper3k, not newspaper. 在python3上你必须安装newspaper3k,而不是报纸。 newspaper is our python2 library. 报纸是我们的python2库。 Although installing newspaper is simple with pip, you will run into fixable issues if you are trying to install on ubuntu. 虽然使用pip安装报纸很简单,但如果你试图在ubuntu上安装,你会遇到可修复的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM