Reputation: 775
website visit using python requests doesn't count in google analytics real time
I am using python requests module and google counts the visit but not found in google analytics real time (active users)
my code is below:
import requests
import time
agent_android = 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36'
headers = {'User-Agent': agent_android}
response = requests.get(url, headers=headers)
print(response.content)
time.sleep(300)
Upvotes: 4
Views: 1502
Reputation: 11
`import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9',
'Host': 'www.google.com',
'Referer': 'https://www.google.com',
'Cookie': 'my_cookie=1234567890; another_cookie=abcdefghij'
}
try:
response = requests.get(url, allow_redirects=False)
if response.status_code == 200:
print('Success')
else:
print('Failed')
except requests.exceptions.ConnectionError:
print('Connection error')
except requests.exceptions.Timeout:
print('Timeout error')
`
Upvotes: 0
Reputation: 189
I suggest you have a look at selenium It's perfect for such purposes. Here an example:
from selenium import webdriver
import time
def main():
url = "https:youURL.com"
driver = webdriver.Firefox()
driver.get(url=url)
time.sleep(300)
driver.quit()
if __name__ == '__main__':
main()
Upvotes: 1