简体   繁体   中英

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. 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. It seems newspaper / request library is missing on your development machine.

$ 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.

Newspaper has been deprecated for Python2 and it's a buggy version according to their official documentation . You should switch to Python3 version.

Installation on Python 3:

pip3 install newspaper3k

Newspaper is a Python3 library! Or, view our deprecated and buggy Python2 branch. Run : pip3 install newspaper3k and NOT pip3 install newspaper. On python3 you must install newspaper3k, not newspaper. newspaper is our python2 library. Although installing newspaper is simple with pip, you will run into fixable issues if you are trying to install on ubuntu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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