utils.web
Utils - web¤
get_page_content(link, session=None, session_query_configs=None, method='GET', data=None)
¤
Download page and save content
Parameters:
Name | Type | Description | Default |
---|---|---|---|
link |
str
|
link to get content from |
required |
session |
Optional[Session]
|
requests session object, defaults to a new session |
None
|
session_query_configs |
Optional[dict]
|
session query configs, defaults to get_session_query_configs |
None
|
method |
Optional[str]
|
method to use, defaults to "GET" |
'GET'
|
data |
Optional[dict]
|
data to send with the request, defaults to None |
None
|
Returns:
Type | Description |
---|---|
page content data |
Source code in kirsche/utils/web.py
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 |
|
get_random_user_agent(browsers=None)
¤
get_random_user_agent returns a random user agent. We provide two predefined browers, chrome and firefox.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
browsers |
Optional[Union[str, list]]
|
which brower to be used, defaults to ["chrome", "firefox"] |
None
|
Returns:
Type | Description |
---|---|
dict
|
dictionary for requests module to consude as {'User-Agent': "blabla"} |
Source code in kirsche/utils/web.py
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 |
|
get_session(retry_params={'retries': 5, 'backoff_factor': 0.3, 'status_forcelist': (500, 502, 504)}, session=None)
¤
get_session prepares a session object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
retry_params |
Optional[dict]
|
the rules to retry, defaults to {"retries": 5, "backoff_factor": 0.3, "status_forcelist": (500, 502, 504)} |
{'retries': 5, 'backoff_factor': 0.3, 'status_forcelist': (500, 502, 504)}
|
session |
Optional[Session]
|
a requests session object to be used to query, defaults to None |
None
|
Returns:
Type | Description |
---|---|
Session
|
a requests session object |
Source code in kirsche/utils/web.py
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 |
|
get_session_query_configs(headers=None, timeout=(5, 14), proxies={}, cookies={'language': 'en'})
¤
get_session_query_configs creates a session config dictionary for session to use. These are the keyword arguments of the session get or post methods. Proxies can be set by providing a dictionary of the form
{
'http': some super_proxy_url,
'https': some super_proxy_url,
}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
headers |
Optional[dict]
|
header of the method such as use agent, defaults to random user agent from get_random_user_agent |
None
|
timeout |
Optional[list]
|
timeout strategy, defaults to (5, 14) |
(5, 14)
|
proxies |
Optional[dict]
|
proxy configs, defaults to {} |
{}
|
cookies |
Optional[dict]
|
cookie configs, defaults to {"language": "en"} |
{'language': 'en'}
|
Returns:
Type | Description |
---|---|
dict
|
dictionary of session configs for session methods, e.g., get, to use. |
Source code in kirsche/utils/web.py
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 |
|