-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_agent_rotator.py
37 lines (30 loc) · 1.02 KB
/
user_agent_rotator.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
from user_agents import user_agent_list
import random
class UserAgentRotator(object):
"""Create a random header from the proxy list
Parameters
----------
path: str
path of the file, the user_agent delimiting the list by new line
"""
random_user_agent = None
def __init__(self, path='scraper/Example.txt'):
self.user_agent = self.get_user_agent()
def get_user_agent(self):
""" Return the User agent """
return self.get_random_user_agent()
def get_random_user_agent(self):
""" Set the `random_user_agent`(user agent and browser info) and Return the User agent """
self.random_user_agent = random.choice(user_agent_list)
return self.random_user_agent['userAgent']
def generate_header(self):
"""
Returns
-------
header: Dict[str]
returns a dict with keys Connection and User-Agent
"""
header = {
"User-Agent": self.user_agent['userAgent']
}
return header