fake_requests_headers

pyhelpers.ops.fake_requests_headers(randomized=True, **kwargs)[source]

Generate fake HTTP headers for requests.get.

This function creates HTTP headers suitable for use with requests.get. By default, it includes a randomly selected user-agent string from various popular browsers.

Parameters:
  • randomized (bool) – Whether to use a randomly selected user-agent string from available browser data; defaults to True; if randomized=False, a random Chrome user-agent string is used.

  • kwargs – [Optional] Additional parameters for the function get_user_agent_string().

Returns:

Fake HTTP headers.

Return type:

dict

Examples:

>>> from pyhelpers.ops import fake_requests_headers
>>> fake_headers_1 = fake_requests_headers()
>>> fake_headers_1
{'user-agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it-IT) AppleWebKit/525.1...
>>> fake_headers_2 = fake_requests_headers(randomized=False)
>>> fake_headers_2  # using a random Chrome user-agent string
{'user-agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.1...

Note

  • fake_headers_1 may also be different every time we run the example. This is because the returned result is randomly chosen from a limited set of candidate user-agent strings, even though randomized is (by default) set to be False.

  • By setting randomized=True, the function returns a random result from among all available user-agent strings of several popular browsers.