get_ip_utils.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re,json
  4. def getIpFromIpaddress(site):
  5. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
  6. 'Host': 'ipaddress.com'}
  7. url = "https://ipaddress.com/search/" + site
  8. trueip = None
  9. try:
  10. res = requests.get(url, headers=headers, timeout=5)
  11. soup = BeautifulSoup(res.text, 'html.parser')
  12. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", res.text)
  13. result = soup.find_all('div', class_="comma-separated")
  14. for c in result:
  15. if len(ip) != 0:
  16. trueip = ip[0]
  17. except Exception as e:
  18. print("查询" + site + " 时出现错误: " + str(e))
  19. return trueip
  20. def getIpFromChinaz(site):
  21. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
  22. 'Host': 'ip.tool.chinaz.com'}
  23. url = "http://ip.tool.chinaz.com/" + site
  24. trueip = None
  25. try:
  26. res = requests.get(url, headers=headers,timeout=5)
  27. soup = BeautifulSoup(res.text, 'html.parser')
  28. result = soup.find_all('span', class_="Whwtdhalf w15-0")
  29. for c in result:
  30. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
  31. if len(ip) != 0:
  32. trueip = ip[0]
  33. except Exception as e:
  34. print("查询" + site + " 时出现错误: " + str(e))
  35. return trueip
  36. def getIpFromWhatismyipaddress(site):
  37. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
  38. 'Host': 'ip.tool.chinaz.com'}
  39. url = "https://whatismyipaddress.com//hostname-ip"
  40. data = {
  41. "DOMAINNAME": site,
  42. "Lookup IP Address": "Lookup IP Address"
  43. }
  44. trueip = None
  45. try:
  46. res = requests.post(url, headers=headers, data=data,timeout=5)
  47. soup = BeautifulSoup(res.text, 'html.parser')
  48. result = soup.find_all('span', class_="Whwtdhalf w15-0")
  49. for c in result:
  50. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
  51. if len(ip) != 0:
  52. trueip = ip[0]
  53. except Exception as e:
  54. print("查询" + site + " 时出现错误: " + str(e))
  55. return trueip
  56. def getIpFromipapi(site):
  57. '''
  58. return trueip: None or ip
  59. '''
  60. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
  61. 'Host': 'ip-api.com'}
  62. url = "http://ip-api.com/json/%s?lang=zh-CN" % (site)
  63. trueip = None
  64. try:
  65. res = requests.get(url, headers=headers,timeout=5)
  66. res=json.loads(res.text)
  67. if(res["status"]=="success"):
  68. trueip=res["query"]
  69. except Exception as e:
  70. print("查询" + site + " 时出现错误: " + str(e))
  71. return trueip