1

I tried to run the code below with Python 2.7.10 Shell:

# encoding: utf8
import urllib2
import re
response=urllib2.urlopen("https://www.baidu.com/")
print response.read()

Unfortunately, I got the error:

Traceback (most recent call last):
  File "C:\Users\Vivid\Desktop\LISA\datacrawling\ex.py", line 6, in <module>
    response=urllib2.urlopen("https://www.baidu.com/")
  File "D:\Program Files\Python2710\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "D:\Program Files\Python2710\lib\urllib2.py", line 431, in open
    response = self._open(req, data)
  File "D:\Program Files\Python2710\lib\urllib2.py", line 449, in _open
    '_open', req)
  File "D:\Program Files\Python2710\lib\urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "D:\Program Files\Python2710\lib\urllib2.py", line 1240, in https_open
    context=self._context)
  File "D:\Program Files\Python2710\lib\urllib2.py", line 1197, in do_open
    raise URLError(err)
URLError: <urlopen error [Errno 11004] getaddrinfo failed>

Do you have any ideas about the problem? I've been caught by it for a long time...Thank you!

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Vito Wong
  • 11
  • 2
  • I don't see the issue. Its likely to do with your config. `getaddrinfo` is using DNS lookup to determine the ip address of baidu.com and thats what not working. I'd suggest that perhaps you search for other similar questions like [this](http://stackoverflow.com/questions/5022945/urllib2-urlerror-urlopen-error-errno-11004-getaddrinfo-failed) which may answer your question. – Paul Rooney Jun 02 '15 at 03:02
  • The DNS lookup failed because of your Internet connection. This has nothing to do with python. – ljk321 Jun 02 '15 at 03:03
  • @PaulRooney Hey Paul, I actually have the same question as the one you post, but none of answers to it could solve the problem well. Clearly, DNS lookup goes wrong, though Internet connection works well. – Vito Wong Jun 02 '15 at 05:54
  • @skyline75489 My Internet connection works well... – Vito Wong Jun 02 '15 at 05:55
  • @VitoWong what happens if you do `ping www.baidu.com` from Windows command prompt? – Paul Rooney Jun 02 '15 at 05:57
  • @PaulRooney `C:\Windows\System32>ping www.baidu.com Pinging www.a.shifen.com [61.135.169.121] with 32 bytes of data: Reply from 61.135.169.121: bytes=32 time=2ms TTL=56 Reply from 61.135.169.121: bytes=32 time=2ms TTL=56 Reply from 61.135.169.121: bytes=32 time=2ms TTL=56 Reply from 61.135.169.121: bytes=32 time=2ms TTL=56 Ping statistics for 61.135.169.121: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 2ms, Maximum = 2ms, Average = 2ms` – Vito Wong Jun 02 '15 at 11:27
  • That works, so it's likely a config issue. I can't reproduce it so I'm sorry I can't help you. I'd suggest googling the issue and also trying some other urls to see if they have the issue. If you need a short term work around you could use the IP address of baidu in your code. I'm not sure how often it changes. – Paul Rooney Jun 02 '15 at 11:31
  • Another link http://stackoverflow.com/questions/8925903/how-to-fix-getaddrinfo-failure-for-python-on-windows – Paul Rooney Jun 02 '15 at 11:33
  • @PaulRooney I've tried many urls even including those without "www", but it still failed to work. I just wanna learn python scrapy and start the simplest code, but...Anyway, it's very nice of you to help me so much. Thank you, Paul! – Vito Wong Jun 02 '15 at 12:20
  • Final advice consider using linux. You could install as VM or dual boot. These issues will disappear. – Paul Rooney Jun 02 '15 at 12:32

0 Answers0