简体   繁体   中英

How do you do requests with proxies?

I have this bit of code that selects a random proxy and a random user-agent from two text files. And it attempts to do a request via those proxies with the user-agent. But it always fails....

Am I doing something wrong?

codeAPI = requests.get(f"https://google.com/", proxies={"https":random_proxy}, headers={"user-agent": random_ua})

The problem could be in the fact that you are using random proxies and user agents and that is why google is blocking your request. I also didn't quite understand what do you use string formatting for.

Try this:

import requests
session = requests.session()
session.proxies = {'http': 'socks5h://127.0.0.1:9150', 'https':'socks5h://127.0.0.1:9150'}
session.headers = {'User-Agent': 'Chrome/94.0.4606.81'}
session.get('https://google.com/')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM