urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>

n1c9
from urllib.request import urlopen
from bs4 import BeautifulSoup
import datetime
import random
import re

random.seed(datetime.datetime.now())

def getLinks(articleUrl):
    html = urlopen("http://en.wikipedia.org"+articleUrl)
    bsObj = BeautifulSoup(html)
    return bsObj.find("div", {"id":"bodyContent"}).findAll("a",href = re.compile("^(/wiki/)((?!:).)*$"))

getLinks('http://en.wikipedia.org')

OS is Linux. The above script spits out a "urllib.error.URLError: ". Looked through a number of attempts to solve this that I found on google, but none of them fixed my problem (attempted solutions include changing the env variable and adding nameserver 8.8.8.8 to my resolv.conf file).

ozgur

You should call getLinks() with a valid url:

>>> getLinks('/wiki/Main_Page')

Besides, in your function, you should also call .read() to get the response content before passing it to BeautifulSoup:

>>> html = urlopen("http://en.wikipedia.org" + articleUrl).read()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What does "urlopen error [Errno -2] Name or service not known" mean?

From Dev

urllib.error.URLError: <urlopen error [Errno 11002] getaddrinfo failed>?

From Dev

URLError: urlopen error [Errno 2] No such file or directory

From Dev

Newbie, error: [Errno -2] Name or service not known

From Dev

Python3 Wolframalpha Giving Error: urllib.error.URLError <urlopen error [Errno 101] Network is unreachable>

From Dev

urllib.error.URLError: <urlopen error unknown url type: 'https>

From Dev

urllib.error.URLError: <urlopen error no host given> python 3

From Java

jupyter throwing error: socket.gaierror: [Errno -2] Name or service not known

From Dev

easy_install not working : Getting error [Errno -2] Name or service not known -- Some packages may not be found

From Dev

jupyter throwing error: socket.gaierror: [Errno -2] Name or service not known

From Dev

easy_install not working : Getting error [Errno -2] Name or service not known -- Some packages may not be found

From Dev

open html in python: urllib.error.URLError: <urlopen error no host given>

From Dev

Python: urllib2 error "Name or server not known"

From Dev

Random "[Errno -2] Name or service not known" errors

From Dev

AWS [Errno -2] Name or service not known

From Dev

AWS [Errno -2] Name or service not known

From Dev

python - getting error (urllib2.URLError) using pandas model

From Dev

Scrapy gives URLError: <urlopen error timed out>

From Dev

SpaCy urllib.error.URLError during Installation

From Dev

python urlopen error [Errno 10060]

From Dev

Python urllib2.urlopen returns a HTTP error 503

From Dev

rabbtimqadmin - Could not connect: [Errno -2] Name or service not known

From Dev

socket.gaierror: [Errno -2] Name or service not known

From Dev

scp + error Name or service not known + custom port

From Dev

urrlib2.urlopen: "Name or service not known" persists when starting script without internet connection

From Dev

urrlib2.urlopen: "Name or service not known" persists when starting script without internet connection

From Dev

urlopen error [Errno 11001] getaddrinfo failed

From Dev

urlopen error [Errno 11001] getaddrinfo failed?

From Java

Import error: No module name urllib2

Related Related

  1. 1

    What does "urlopen error [Errno -2] Name or service not known" mean?

  2. 2

    urllib.error.URLError: <urlopen error [Errno 11002] getaddrinfo failed>?

  3. 3

    URLError: urlopen error [Errno 2] No such file or directory

  4. 4

    Newbie, error: [Errno -2] Name or service not known

  5. 5

    Python3 Wolframalpha Giving Error: urllib.error.URLError <urlopen error [Errno 101] Network is unreachable>

  6. 6

    urllib.error.URLError: <urlopen error unknown url type: 'https>

  7. 7

    urllib.error.URLError: <urlopen error no host given> python 3

  8. 8

    jupyter throwing error: socket.gaierror: [Errno -2] Name or service not known

  9. 9

    easy_install not working : Getting error [Errno -2] Name or service not known -- Some packages may not be found

  10. 10

    jupyter throwing error: socket.gaierror: [Errno -2] Name or service not known

  11. 11

    easy_install not working : Getting error [Errno -2] Name or service not known -- Some packages may not be found

  12. 12

    open html in python: urllib.error.URLError: <urlopen error no host given>

  13. 13

    Python: urllib2 error "Name or server not known"

  14. 14

    Random "[Errno -2] Name or service not known" errors

  15. 15

    AWS [Errno -2] Name or service not known

  16. 16

    AWS [Errno -2] Name or service not known

  17. 17

    python - getting error (urllib2.URLError) using pandas model

  18. 18

    Scrapy gives URLError: <urlopen error timed out>

  19. 19

    SpaCy urllib.error.URLError during Installation

  20. 20

    python urlopen error [Errno 10060]

  21. 21

    Python urllib2.urlopen returns a HTTP error 503

  22. 22

    rabbtimqadmin - Could not connect: [Errno -2] Name or service not known

  23. 23

    socket.gaierror: [Errno -2] Name or service not known

  24. 24

    scp + error Name or service not known + custom port

  25. 25

    urrlib2.urlopen: "Name or service not known" persists when starting script without internet connection

  26. 26

    urrlib2.urlopen: "Name or service not known" persists when starting script without internet connection

  27. 27

    urlopen error [Errno 11001] getaddrinfo failed

  28. 28

    urlopen error [Errno 11001] getaddrinfo failed?

  29. 29

    Import error: No module name urllib2

HotTag

Archive