|
1
|
|
|
from types import SimpleNamespace |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import requests |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
UserAgent = SimpleNamespace( |
|
|
|
|
|
|
7
|
|
|
aol='Mozilla/4.0 (compatible; MSIE 4.01; AOL 4.0; Windows 95)', |
|
8
|
|
|
ie='Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)', |
|
9
|
|
|
chrome='Mozilla/5.0 (Windows NT 6.1) AppleWebKit/530.10 (KHTML, like Gecko) Chrome/ Safari/530.5', |
|
|
|
|
|
|
10
|
|
|
safari='Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit (KHTML, like Gecko)', |
|
11
|
|
|
firefox='Mozilla/5.0 (Windows 95; en-US; rv:1.8.1.13) Gecko/20080313 Firefox', |
|
12
|
|
|
ios='mozilla/5.0 (iphone; cpu iphone os 8_0_0 like mac os x) applewebkit/537.51.1 (KHTML, like Gecko) version/7.0 mobile/11a501 safari/9537.53', |
|
|
|
|
|
|
13
|
|
|
android='Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9' |
|
|
|
|
|
|
14
|
|
|
) |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class Agent: |
|
|
|
|
|
|
18
|
|
|
def __init__(self, user_agent='chrome'): |
|
19
|
|
|
self.session = requests.session() |
|
20
|
|
|
self.user_agent = user_agent |
|
21
|
|
|
self.set_user_agent(self.user_agent) |
|
22
|
|
|
|
|
23
|
|
|
def set_header(self, params): |
|
24
|
|
|
self.session.headers.update(params) |
|
25
|
|
|
|
|
26
|
|
|
def get_header(self, name): |
|
27
|
|
|
return self.session.headers[name] |
|
28
|
|
|
|
|
29
|
|
|
def get(self, url): |
|
30
|
|
|
return self.session.get(url) |
|
31
|
|
|
|
|
32
|
|
|
def post(self, url, **kwargs): |
|
33
|
|
|
return self.session.post(url, **kwargs) |
|
34
|
|
|
|
|
35
|
|
|
def set_user_agent(self, agent): |
|
36
|
|
|
agent_string = getattr(UserAgent, agent) |
|
37
|
|
|
params = {'User-Agent': agent_string} |
|
38
|
|
|
self.set_header(params) |
|
39
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.