Proxy Random is a tool to help small web scrapers. helping them prevent getting their ip banned from the target site.
default proxy lists are free-proxy-list.net and sslproxies.org but you can also add your desired proxy list websites and add a extractor for that website.
Installation
$ pip install proxy-random
Documentation
Usage
here are some examples on how to use proxy-random.
Example 1:
import requests
from proxy_random import RandomProxy
# if you want to use a proxy to load the proxy lists
# use this instead(rp = RandomProxy(proxy='http://example.com:8080'))
rp = RandomProxy()
proxies = rp.extract_proxies() # you can also use rp.proxy_query.
# filter the US proxies which use port 80 or 443 and check if they work
# you can filter by multiple parameters at once or provide your own filter function(s)
workings = proxies.filter(port=[80, 443]) \
.check_health(timeout=5).filter(working=True)
print(workings.random().url) # print a random working proxy
# or iterate through proxies and use them
for proxy in workings:
# do something with the proxy
requests.get("https://httpbin.org/ip", proxies={"http": proxy.url, "https": proxy.url})
ProxyQuery(s) are reusable so you can filter them as many times as needed.
here is another example of how to add custom providers
Example 2:
import requests
from bs4 import BeautifulSoup
from proxy_random import RandomProxy
from proxy_random.provider import Provider
from proxy_random.query import ProxyQuery
from proxy_random.proxy import Proxy
# you can also use RandomProxy(use_defaults=False) to disable default providers
rp = RandomProxy()
# add a custom provider
url = "https://free-proxy-list.net" # the url of the proxy list
# the function used to extract proxies from the url response
def extract_proxies(response: str) -> ProxyQuery:
soup = BeautifulSoup(response, "html.parser")
headings = [i.text.lower() for i in soup.find("thead").find_all("th")]
rows = [[j.text for j in i] for i in soup.find("tbody").find_all("tr")]
proxies = []
for row in rows:
proxy = Proxy()
for i, name in enumerate(headings):
if name == "ip address":
proxy.ip = row[i]
elif name == "port":
proxy.port = int(row[i])
elif name == "code":
proxy.country_code = row[i]
elif name == "last checked":
proxy.last_checked = row[i]
elif name in ("google", "https"):
setattr(proxy, name, True if row[i] == "yes" else False)
elif name in ("country", "anonymity"):
setattr(proxy, name, row[i])
proxies.append(proxy)
return ProxyQuery(proxies)
# then create a new instance of the Provider class
provider = Provider(url=url, extractor=extract_proxies)
# then add the provider to the RandomProxy instance
rp.add_provider(provider)
# then extract the proxies like example 1
rp.extract_proxies()
...
My own usage of this package:
import requests
from proxy_random import RandomProxy
rp = RandomProxy(proxy="my proxy")
proxies = rp.extract_proxies()
workings = proxies.filter(custom_filters=[lambda x: x.country_code != "ir",]) \
.limit(50).check_health(timeout=5).filter(working=True)
proxy = workings.random()
# use the proxy in some way
...
Refer to the documentation for more information about these classes.
Metadata
Release files for proxy-random 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| proxy_random-0.0.2.tar.gz | 10.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| proxy_random-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.6 kB
Release files / proxy_random-0.0.2.tar.gz
| Download URL | proxy_random-0.0.2.tar.gz |
|---|---|
| Size | 10.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
47af3c3cebe3589b4e6fa13638ce82d2c876bf50c16a5834b89fcb82d10f824f
|
|
BLAKE2b-256 checksum How to use checksums |
47d75f997c4ba6b428dfa94c49a8f8c0ee5fbc36298cf47be73b02f73e84669d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0
|
Release files / proxy_random-0.0.2-py3-none-any.whl
| Download URL | proxy_random-0.0.2-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
72e175d91a465785e29e92f85836070fa06112a2bc2c6d9a1292cc28e68ff459
|
|
BLAKE2b-256 checksum How to use checksums |
762c353c2e0124ffc3dbd869283f22631ac2286af0b714ea9cc81d6ae402ae67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.0
|
Security for proxy-random 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.