I want to set proxies to my crawler. I'm using requests module and Beautiful Soup. I have found a list of API links that provide free proxies with 4 types of protocols.
All proxies with 3/4 protocols work (HTTP, SOCKS4, SOCKS5) except one, and thats proxies with HTTPS protocol. This is my code:
from bs4 import BeautifulSoup
import requests
import random
import json
# LIST OF FREE PROXY APIS, THESE PROXIES ARE LAST TIME TESTED 50 MINUTES AGO, PROTOCOLS: HTTP, HTTPS, SOCKS4 AND SOCKS5
list_of_proxy_content = ["https://proxylist.geonode.com/api/proxy-list?limit=150&page=1&sort_by=lastChecked&sort_type=desc&filterLastChecked=50&country=CH&protocols=http%2Chttps%2Csocks4%2Csocks5",
"https://proxylist.geonode.com/api/proxy-list?limit=150&page=1&sort_by=lastChecked&sort_type=desc&filterLastChecked=50&country=FR&protocols=http%2Chttps%2Csocks4%2Csocks5",
"https://proxylist.geonode.com/api/proxy-list?limit=150&page=1&sort_by=lastChecked&sort_type=desc&filterLastChecked=50&country=DE&protocols=http%2Chttps%2Csocks4%2Csocks5",
"https://proxylist.geonode.com/api/proxy-list?limit=1500&page=1&sort_by=lastChecked&sort_type=desc&filterLastChecked=50&country=AT&protocols=http%2Chttps%2Csocks4%2Csocks5",
"https://proxylist.geonode.com/api/proxy-list?limit=150&page=1&sort_by=lastChecked&sort_type=desc&filterLastChecked=50&country=IT&protocols=http%2Chttps%2Csocks4%2Csocks5"]
# EXTRACTING JSON DATA FROM THIS LIST OF PROXIES
full_proxy_list = []
for proxy_url in list_of_proxy_content:
proxy_json = requests.get(proxy_url).text
proxy_json = json.loads(proxy_json)
proxy_json = proxy_json["data"]
full_proxy_list.extend(proxy_json)
# CREATING PROXY DICT
final_proxy_list = []
for proxy in full_proxy_list:
#print(proxy) # JSON VALUE FOR ALL DATA THAT GOES INTO PROXY
protocol = proxy['protocols'][0]
ip_ = proxy['ip']
port = proxy['port']
proxy = {protocol : protocol + '://' + ip_ + ':' + port}
final_proxy_list.append(proxy)
# TRYING PROXY ON 3 DIFERENT WEBSITES
for proxy in final_proxy_list:
print(proxy)
try:
r0 = requests.get("https://edition.cnn.com/", proxies=proxy, timeout = 15)
if r0.status_code == 200:
print("GOOD PROXY")
else:
print("BAD PROXY")
except:
print("proxy error")
try:
r1 = requests.get("https://www.buelach.ch/", proxies=proxy, timeout = 15)
if r1.status_code == 200:
print("GOOD PROXY")
else:
print("BAD PROXY")
except:
print("proxy error")
try:
r2 = requests.get("https://www.blog.police.be.ch/", proxies=proxy, timeout = 15)
if r2.status_code == 200:
print("GOOD PROXY")
else:
print("BAD PROXY")
except:
print("proxy error")
print()
My question is, why does HTTPS proxies do not work, what am I doing wrong?
My proxies look like this:
{'socks4': 'socks4://185.168.173.35:5678'}
{'http': 'http://62.171.177.80:3128'}
{'https': 'http://159.89.28.169:3128'}
I have seen that sometimes people pass proxies like this:
proxies = {"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080"}
But this dict has 2 protocols, but in links its only http, why? Can I pass only one, can I pass 10 different IP addresses in this dict?