-
Notifications
You must be signed in to change notification settings - Fork 22
/
url.py
178 lines (151 loc) · 5.56 KB
/
url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
To configure Privoxy+Tor:
# Check tor:
tsocks lynx https://check.torproject.org/
(I couldn't get Polipo working, so I used Privoxy)
https://groups.google.com/forum/?fromgroups#!topic/scrapy-users/WqMLnKbA43I
Make sure tor is running with control mode:
In [1]: import telnetlib
In [2]: tn = telnetlib.Telnet('127.0.0.1', 9051)
If not, edit the torrc:
SocksPort 9050 # what port to open for local application connections
SocksListenAddress 127.0.0.1 # accept connections only from localhost
RunAsDaemon 1
ControlPort 9051
Remember to edit /etc/socks/tsocks.conf (provided by tor)
and privoxy's config. Read the docs, it should at least include: forward-socks4a / localhost:9050 .
Start the spider with *tsocks* and persistence:
tsocks scrapy crawl fullsite -s JOBDIR=crawls/fullsitespider-`date +'%F-%T'`
"""
import httplib
import urllib2
import sys
httplib.HTTPConnection.debuglevel = 1
#useragent = 'Mozilla/4.51 (Macintosh; U; PPC)'
useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'
# see these: http://techpatterns.com/forums/about304.html
from common.str import percent
from StringIO import StringIO
import gzip
import telnetlib
TOR_WORKS = False
#def fetch(url, decode=False, timeout=15):
def fetch(url, decode=True, timeout=15):
"""
Return the text of a particular URL
If decode, then attempt to decode the text.
"""
request = urllib2.Request(url)
request.get_full_url()
request.add_header('User-Agent', useragent)
request.add_header('Accept-Encoding', 'gzip')
opener = urllib2.build_opener()
response = opener.open(request, timeout=timeout)
if decode:
return decode_response(response)
else:
data = response.read()
return data
def torfetch(url, decode=True, timeout=60, retries=5):
torcheck()
for i in range(retries):
try:
html = _torfetch(url, decode=decode, timeout=timeout)
return html
except Exception, e:
print >> sys.stderr, "torfetch(), try %s: %s %" % (percent(i+1, retries), type(e), e)
if i < retries-1:
torchange()
else:
raise
def torcheck():
"""
Check that tor is working.
"""
global TOR_WORKS
if not TOR_WORKS:
assert _torfetch("https://check.torproject.org/").find("Congratulations. Your browser is configured to use Tor.") != -1
TOR_WORKS = True
def torchange():
"""
Change the tor nym.
"""
# log.msg('Changing proxy', level=log.INFO)
print >> sys.stderr, "Changing proxy"
tn = telnetlib.Telnet('127.0.0.1', 9051)
tn.read_until("Escape character is '^]'.", 2)
tn.write('AUTHENTICATE "267765"\r\n')
tn.read_until("250 OK", 2)
tn.write("signal NEWNYM\r\n")
tn.read_until("250 OK", 2)
tn.write("quit\r\n")
tn.close()
# time.sleep(3)
# log.msg('Proxy changed', level=log.INFO)
print >> sys.stderr, "Proxy changed"
# Check that Tor works again.
TOR_WORKS = None
torcheck()
def _torfetch(url, decode=True, timeout=60):
# This is broken
proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118", "https": "127.0.0.1:8118"})
opener = urllib2.build_opener(proxy_support)
opener.addheaders = [('User-agent', useragent), ('Accept-Encoding', 'gzip')]
response = opener.open(url, timeout=timeout)
if decode:
return decode_response(response)
else:
data = response.read()
return data
def decode_response(response):
"""
The proper decoding technique is described here:
http://stackoverflow.com/questions/1495627/how-to-download-any-webpage-with-correct-charset-in-python
We skip some of the steps, though.
1. See if there is a charset in the HTTP header.
2. [skipped] An encoding discovered in the document itself: for
instance, in an XML declaration or (for HTML documents) an http-equiv
META tag. If Beautiful Soup finds this kind of encoding within the
document, it parses the document again from the beginning and gives
the new encoding a try. The only exception is if you explicitly
specified an encoding, and that encoding actually worked: then it
will ignore any encoding it finds in the document.
3. chardet predicted encoding.
4. utf-8
5. windows-1252
"""
text = response.read()
# Attempt to ungzip
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO(text)
f = gzip.GzipFile(fileobj=buf)
text = f.read()
charset = response.headers.getparam('charset')
if charset is not None:
(worked, decoded_text) = attempt_decode(charset, text)
if worked: return decoded_text
# BeautifulSoup goes here
# Try chardet
import chardet
charset = chardet.detect(text)["encoding"]
if charset is not None:
(worked, decoded_text) = attempt_decode(charset, text)
if worked: return decoded_text
# Try utf-8
(worked, decoded_text) = attempt_decode("utf-8", text)
if worked: return decoded_text
# Try windows-1252
(worked, decoded_text) = attempt_decode("windows-1252", text)
if worked: return decoded_text
assert 0
def attempt_decode(charset, text):
"""
Attempt to decode text with this charset.
Return (worked, decoded_text), where worked is True if we could successfully decode.
"""
try:
decoded_text = text.decode(charset)
return (True, decoded_text)
except Exception, e:
print >> sys.stderr, "Could not decode with %s, SKIPPING." % charset, type(e), e
return (False, None)