get_ip_utils.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  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=2)
  11. soup = BeautifulSoup(res.text, 'html.parser')
  12. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.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)
  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)
  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