3

I am new to python and I learned there are no different between single and double quoted string. But I have found some different behavior.

from bs4 import BeautifulSoup
import urllib.request

url1 = "http://www.backpackers.com.tw/forum/forumdisplay.php?f=310"
url2 = 'http://www.backpackers.com.tw/forum/forumdisplay.php?f=310'

If I run:

response = urllib.request.urlopen(url1)

Result: Script finished without error

And If I run:

response = urllib.request.urlopen(url2)

Result: error

C:\Users\user1\Desktop\scrape>python backpacker_tw.py
Traceback (most recent call last):
  File "C:\Python34\lib\urllib\request.py", line 1189, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "C:\Python34\lib\http\client.py", line 1090, in request
    self._send_request(method, url, body, headers)
  File "C:\Python34\lib\http\client.py", line 1128, in _send_request
    self.endheaders(body)
  File "C:\Python34\lib\http\client.py", line 1086, in endheaders
    self._send_output(message_body)
  File "C:\Python34\lib\http\client.py", line 924, in _send_output
    self.send(msg)
  File "C:\Python34\lib\http\client.py", line 859, in send
    self.connect()
  File "C:\Python34\lib\http\client.py", line 836, in connect
    self.timeout, self.source_address)
  File "C:\Python34\lib\socket.py", line 509, in create_connection
    raise err
  File "C:\Python34\lib\socket.py", line 500, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the
 target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "backpacker_tw.py", line 7, in <module>
    response = urllib.request.urlopen(url2)
  File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python34\lib\urllib\request.py", line 455, in open
    response = self._open(req, data)
  File "C:\Python34\lib\urllib\request.py", line 473, in _open
    '_open', req)
  File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "C:\Python34\lib\urllib\request.py", line 1215, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python34\lib\urllib\request.py", line 1192, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10061] No connection could be ma
de because the target machine actively refused it>

Is it a bug or anything i missed?

C:\Users\user1\Desktop\scrape>python -V
Python 3.4.1
user
  • 5,370
  • 8
  • 47
  • 75
feleio
  • 1,170
  • 1
  • 6
  • 6
  • 5
    They both work for me. Did you retry? It's possible that it just failed the second time due to some connection or server glitch. – BrenBarn Feb 28 '15 at 07:22
  • If I believe http://en.wikipedia.org/wiki/Percent-encoding, the apostrophe is a valid URI character, however the latest RFC says it a reserved character that should be encoded as %27 (it actually works in my browser when encoded this way). – m0bi5 Feb 28 '15 at 07:24
  • @MohitBhasi: Sure, apostrophes in a URL should be percent-encoded, but there's no apostrophe in that URL. FWIW, when you pass a string literal to a function, the function just receives the string characters, not the delimiters - there's no way for the function to actually know how you wrote the string, whether you use `'`, `"` or triple-quoting, or whether it was created dynamically by some `str` method, etc. – PM 2Ring Feb 28 '15 at 07:54
  • Maybe the site doesn't like you using a script to access it. Maybe try changing the [User-Agent](http://stackoverflow.com/questions/802134/changing-user-agent-on-urllib2-urlopen) – Peter Wood Feb 28 '15 at 07:55

1 Answers1

3

To the documentation! PEP 8, pretty much the be all end all on python code formatting, states "In Python, single-quoted strings and double-quoted strings are the same." This is written by the creator of Python, and I'm going to take his word for it.

Looking at your stack trace I see the error No connection could be ma de because the target machine actively refused it, so maybe that means something wonky was going on with the server at that point?

Others
  • 2,876
  • 2
  • 30
  • 52