-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoscraper_proxy.py
More file actions
344 lines (279 loc) · 11.1 KB
/
Copy pathautoscraper_proxy.py
File metadata and controls
344 lines (279 loc) · 11.1 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
AutoScraper extension for sending and receiving proxy headers.
This module provides an AutoScraper subclass that enables:
1. Sending custom headers to proxy servers during CONNECT
2. Using our ProxySession for all HTTP requests
Example usage:
from python_proxy_headers.autoscraper_proxy import ProxyAutoScraper
scraper = ProxyAutoScraper(proxy_headers={'X-ProxyMesh-Country': 'US'})
# Build with proxy
result = scraper.build(
url='https://example.com',
wanted_list=['Example Domain'],
request_args={'proxies': {'https': 'http://proxy:8080'}}
)
# Get results with proxy
result = scraper.get_result_similar(
url='https://other-example.com',
request_args={'proxies': {'https': 'http://proxy:8080'}}
)
"""
from typing import Dict, List, Optional, Any
from urllib.parse import urlparse
try:
from autoscraper import AutoScraper
except ImportError:
raise ImportError(
"autoscraper is required for this module. "
"Install it with: pip install autoscraper"
)
from .requests_adapter import ProxySession
class ProxyAutoScraper(AutoScraper):
"""
AutoScraper with proxy header support.
This class extends AutoScraper to use our ProxySession for HTTP requests,
enabling custom proxy headers to be sent during CONNECT tunneling.
Args:
proxy_headers: Dict of headers to send to proxy servers
stack_list: Initial stack list (rules) for the scraper
Example:
scraper = ProxyAutoScraper(proxy_headers={'X-ProxyMesh-Country': 'US'})
result = scraper.build(
url='https://finance.yahoo.com/quote/AAPL/',
wanted_list=['Apple Inc.'],
request_args={'proxies': {'https': 'http://proxy:8080'}}
)
# Use the learned rules on another page
result = scraper.get_result_similar(
url='https://finance.yahoo.com/quote/GOOG/',
request_args={'proxies': {'https': 'http://proxy:8080'}}
)
"""
def __init__(
self,
proxy_headers: Optional[Dict[str, str]] = None,
stack_list: Optional[List] = None
):
super().__init__(stack_list=stack_list)
self._proxy_headers = proxy_headers or {}
self._session: Optional[ProxySession] = None
def _get_session(self) -> ProxySession:
"""Get or create the ProxySession."""
if self._session is None:
self._session = ProxySession(proxy_headers=self._proxy_headers)
return self._session
def set_proxy_headers(self, proxy_headers: Dict[str, str]):
"""
Update the proxy headers.
This will close the current session and create a new one with
the updated headers on the next request.
Args:
proxy_headers: New proxy headers to use
"""
self._proxy_headers = proxy_headers
if self._session is not None:
self._session.close()
self._session = None
def close(self):
"""Close the underlying session."""
if self._session is not None:
self._session.close()
self._session = None
def __enter__(self):
return self
def __exit__(self, *args):
self.close()
@classmethod
def _fetch_html(cls, url, request_args=None):
"""
Fetch HTML from URL using the standard requests.
Note: This is the class method from parent. For proxy header support,
use instance methods which use the ProxySession.
"""
# Fall back to parent implementation for class method calls
return super()._fetch_html(url, request_args)
def _fetch_html_with_proxy(self, url: str, request_args: Optional[Dict] = None) -> str:
"""
Fetch HTML from URL using ProxySession with proxy header support.
Args:
url: URL to fetch
request_args: Additional request arguments (proxies, headers, etc.)
Returns:
HTML content as string
"""
request_args = request_args or {}
# Build headers
headers = dict(self.request_headers)
if url:
headers["Host"] = urlparse(url).netloc
user_headers = request_args.pop("headers", {})
headers.update(user_headers)
# Use our ProxySession
session = self._get_session()
# Copy session-level settings if not in request_args
if 'proxies' in request_args:
session.proxies.update(request_args.pop('proxies'))
res = session.get(url, headers=headers, **request_args)
# Handle encoding
if res.encoding == "ISO-8859-1" and "ISO-8859-1" not in res.headers.get(
"Content-Type", ""
):
res.encoding = res.apparent_encoding
return res.text
def _get_soup_with_proxy(self, url=None, html=None, request_args=None):
"""
Get BeautifulSoup object using ProxySession.
Args:
url: URL to fetch (optional if html is provided)
html: HTML string (optional if url is provided)
request_args: Additional request arguments
Returns:
BeautifulSoup object
"""
from html import unescape
from bs4 import BeautifulSoup
from autoscraper.utils import normalize
if html:
html = normalize(unescape(html))
return BeautifulSoup(html, "lxml")
html = self._fetch_html_with_proxy(url, request_args)
html = normalize(unescape(html))
return BeautifulSoup(html, "lxml")
def build(
self,
url: Optional[str] = None,
wanted_list: Optional[List] = None,
wanted_dict: Optional[Dict] = None,
html: Optional[str] = None,
request_args: Optional[Dict] = None,
update: bool = False,
text_fuzz_ratio: float = 1.0,
) -> List:
"""
Build scraping rules with proxy header support.
Same as AutoScraper.build() but uses ProxySession for requests.
Parameters:
url: URL of the target web page
wanted_list: List of needed contents to be scraped
wanted_dict: Dict of needed contents (keys are aliases)
html: HTML string (alternative to URL)
request_args: Request arguments including proxies
update: If True, add to existing rules
text_fuzz_ratio: Fuzziness ratio for matching
Returns:
List of similar results
"""
from html import unescape
from autoscraper.utils import normalize, unique_hashable, unique_stack_list
if not wanted_list and not (wanted_dict and any(wanted_dict.values())):
raise ValueError("No targets were supplied")
# Use our proxy-aware soup getter
soup = self._get_soup_with_proxy(url=url, html=html, request_args=request_args)
result_list = []
if update is False:
self.stack_list = []
if wanted_list:
wanted_dict = {"": wanted_list}
wanted_list = []
for alias, wanted_items in wanted_dict.items():
wanted_items = [normalize(w) for w in wanted_items]
wanted_list += wanted_items
for wanted in wanted_items:
children = self._get_children(soup, wanted, url, text_fuzz_ratio)
for child in children:
result, stack = self._get_result_for_child(child, soup, url)
stack["alias"] = alias
result_list += result
self.stack_list.append(stack)
result_list = [item.text for item in result_list]
result_list = unique_hashable(result_list)
self.stack_list = unique_stack_list(self.stack_list)
return result_list
def get_result_similar(
self,
url: Optional[str] = None,
html: Optional[str] = None,
soup=None,
request_args: Optional[Dict] = None,
grouped: bool = False,
group_by_alias: bool = False,
unique: Optional[bool] = None,
attr_fuzz_ratio: float = 1.0,
keep_blank: bool = False,
keep_order: bool = False,
contain_sibling_leaves: bool = False,
):
"""
Get similar results with proxy header support.
Same as AutoScraper.get_result_similar() but uses ProxySession.
"""
if soup is None and url is not None:
soup = self._get_soup_with_proxy(url=url, html=html, request_args=request_args)
return super().get_result_similar(
url=url,
html=html,
soup=soup,
request_args=None, # Already fetched
grouped=grouped,
group_by_alias=group_by_alias,
unique=unique,
attr_fuzz_ratio=attr_fuzz_ratio,
keep_blank=keep_blank,
keep_order=keep_order,
contain_sibling_leaves=contain_sibling_leaves,
)
def get_result_exact(
self,
url: Optional[str] = None,
html: Optional[str] = None,
soup=None,
request_args: Optional[Dict] = None,
grouped: bool = False,
group_by_alias: bool = False,
unique: Optional[bool] = None,
attr_fuzz_ratio: float = 1.0,
keep_blank: bool = False,
):
"""
Get exact results with proxy header support.
Same as AutoScraper.get_result_exact() but uses ProxySession.
"""
if soup is None and url is not None:
soup = self._get_soup_with_proxy(url=url, html=html, request_args=request_args)
return super().get_result_exact(
url=url,
html=html,
soup=soup,
request_args=None, # Already fetched
grouped=grouped,
group_by_alias=group_by_alias,
unique=unique,
attr_fuzz_ratio=attr_fuzz_ratio,
keep_blank=keep_blank,
)
def get_result(
self,
url: Optional[str] = None,
html: Optional[str] = None,
request_args: Optional[Dict] = None,
grouped: bool = False,
group_by_alias: bool = False,
unique: Optional[bool] = None,
attr_fuzz_ratio: float = 1.0,
):
"""
Get similar and exact results with proxy header support.
Same as AutoScraper.get_result() but uses ProxySession.
"""
soup = self._get_soup_with_proxy(url=url, html=html, request_args=request_args)
args = dict(
url=url,
soup=soup,
grouped=grouped,
group_by_alias=group_by_alias,
unique=unique,
attr_fuzz_ratio=attr_fuzz_ratio,
)
similar = self.get_result_similar(**args)
exact = self.get_result_exact(**args)
return similar, exact