import requests
from bs4 import BeautifulSoup
url = "http://www.whatsmyip.org"
for x in range(0,5):
response = requests.get(url).content
soup = BeautifulSoup(response,'lxml')
result = soup.findAll('h1')
for each in result:
print each.text
break
Output:
Your IP Address is 19.12.86.57
Your IP Address is 151.138.87.69
Your IP Address is 108.206.165.11
Your IP Address is 148.84.71.226
Your IP Address is 50.201.205.131
Asked
Active
Viewed 364 times
1

Varun Talikoti
- 11
- 1
-
Do you use something like tor? – syntonym Apr 29 '16 at 09:00
-
No I am not. Just puzzled with this response (output) – Varun Talikoti Apr 29 '16 at 09:03
-
Same for me, have you found out why yet? – Kevin Jun 12 '17 at 08:04
1 Answers
1
I think its not about python, but about whatsmyip.org ;) Some method probably in which they detect and try to prevent scripting.
Tried some other website and always got my public IP. Example:
url = "https://www.iplocation.net"
for x in range(0,5):
response = requests.get(url).content
soup = BeautifulSoup(response, 'html.parser')
result = soup.findAll('span')
for each in result:
try:
if each.text[0] in '01234567890':
print(each.text)
break
except:
continue

Roelant
- 4,508
- 1
- 32
- 62