main.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from bs4 import BeautifulSoup
  2. import time
  3. import requests
  4. from random import choice
  5. def get_proxy():
  6. html = requests.get('https://free-proxy-list.net/').text
  7. soup = BeautifulSoup(html, 'lxml')
  8. # trs = soup.find('tbody').find_all('tr')[1:11]
  9. trs = soup.find('tbody').find_all('tr')
  10. proxies = []
  11. for tr in trs:
  12. tds = tr.find_all('td')
  13. ip = tds[0].text.strip()
  14. port = tds[1].text.strip()
  15. schema = "https" if 'yes' in tds[6].text.strip() else 'http'
  16. data = {"schema": schema, "address": ip+":"+port}
  17. proxies.append(data)
  18. return choice(proxies)
  19. def time_watcher():
  20. print('--- %s seconds ---' % (time.time() - start_time))
  21. def get_html(url):
  22. # proxies = {'https': 'ipaddress:5000'}
  23. p = get_proxy() # {"schema": '', "address": ""}
  24. proxy = {p['schema']: p['address']}
  25. response = requests.get(url, proxies=proxy, timeout=5)
  26. return response.json()['origin']
  27. def main():
  28. url = 'http://httpbin.org/ip'
  29. result = get_html(url)
  30. print(result)
  31. if __name__ == '__main__':
  32. start_time = time.time()
  33. main()
  34. time_watcher()