帮助文档>代理IP>代码示例 > 代码示例-Python

代码示例-Python

发布时间:2021-08-03 21:04

Python requests

  1. import requests
  2. targetURL = "https://ip.cn/api/index?ip=&type=0"
  3. proxyAddr = "您的代理IP:端口"
  4. authKey = "请改成您的Key"
  5. password = "请改成您的AuthPwd"
  6. # 账密模式
  7. proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
  8. "user": authKey,
  9. "password": password,
  10. "server": proxyAddr,
  11. }
  12. proxies = {
  13. "http": proxyUrl,
  14. "https": proxyUrl,
  15. }
  16. resp = requests.get(targetURL, proxies=proxies)
  17. print(resp.text)

Python aiohttp

  1. import aiohttp,asyncio
  2. targetURL = "https://ip.cn/api/index?ip=&type=0"
  3. proxyAddr = "您的代理IP:端口"
  4. authKey = "请改成您的Key"
  5. password = "请改成您的AuthPwd"
  6. # 账密模式
  7. proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
  8. "user": authKey,
  9. "password": password,
  10. "server": proxyAddr,
  11. }
  12. async def entry():
  13. conn = aiohttp.TCPConnector(ssl=False)
  14. async with aiohttp.ClientSession(connector=conn) as session:
  15. async with session.get(targetURL, proxy=proxyUrl) as resp:
  16. body = await resp.read()
  17. print(resp.status)
  18. print(body)
  19. loop = asyncio.get_event_loop()
  20. loop.run_until_complete(entry())
  21. loop.run_forever()

Python urllib2、urllib

  1. import urllib2
  2. targetURL = "https://ip.cn/api/index?ip=&type=0"
  3. proxyAddr = "您的代理IP:端口"
  4. authKey = "请改成您的Key"
  5. password = "请改成您的AuthPwd"
  6. proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
  7. "user": authKey,
  8. "password": password,
  9. "server": proxyAddr,
  10. }
  11. proxies = urllib2.ProxyHandler({
  12. "http": proxyUrl,
  13. "https": proxyUrl,
  14. })
  15. opener = urllib2.build_opener(proxies)
  16. urllib2.install_opener(opener)
  17. resp = urllib2.urlopen(targetURL).read()
  18. print(resp)
本文导读

客户热线: