Last active
August 29, 2015 14:12
-
-
Save elleryq/aff74c950863cad69faa to your computer and use it in GitHub Desktop.
Pick a server from top 10 proxy servers from http://cn-proxy.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Dependencies: | |
* requests | |
* pyquery | |
ChangeLog: | |
* 2015/1/5 Check the length of returned proxy. | |
* 2014/12/27 Add seed() make sure randomize. Rename main to get_proxy_from_cnproxy for better using. | |
* 2014/12/27 Initial. | |
""" | |
from __future__ import print_function, unicode_literals | |
import requests | |
from pyquery.pyquery import PyQuery | |
from random import randint, seed | |
seed() | |
def get_proxy_from_cnproxy(): | |
r = requests.get("http://cn-proxy.com/") | |
q = PyQuery(r.content) | |
trs = q("tbody tr") | |
proxies = [] | |
for tr in trs: | |
trq = PyQuery(tr) | |
tds = trq.children() | |
ip = tds.eq(0).text() | |
port = tds.eq(1).text() | |
proxies.append((ip, port)) | |
top10proxies = proxies[:10] | |
n = min(10, len(top10proxies)) | |
if n: | |
ip, port = top10proxies[randint(0, n)] | |
else: | |
ip = "" | |
port = "" | |
return (ip, port) | |
if __name__ == "__main__": | |
proxy = get_proxy_from_cnproxy() | |
ip, port = proxy | |
print("{0}:{1}".format(ip, port)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment