Python/Python Challenge
[Python Challenge]파이썬 챌린지 Level 4 풀이
Mr.robot 2
2022. 7. 20. 12:00
Level 4를 들어가니 이상한 그림이 나왔다. 이전 Level 과 동일하게 페이지 소스 보기를 하니 아래와 같은 소스가 나왔다.
주석 내용은 urllib를 사용하는 것이 도움이 될 것이고 끝날때 까지 계속 시도하라 400번이 넘는 것이 필요할 것이다.
라고 적혀 있다.
그리고 href로 다음 링크로 안내해주는 코드가 있었다.
<html>
<head>
<title>follow the chain</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never
end. 400 times is more than enough. -->
<center>
<a href="linkedlist.php?nothing=12345"><img src="chainsaw.jpg" border="0"/></a>
<br><br><font color="gold"></center>
Solutions to previous levels: <a href="http://wiki.pythonchallenge.com/"/>Python Challenge wiki</a>.
<br><br>
IRC: irc.freenode.net #pythonchallenge
</body>
</html>
해당 링크로 들어가니 아래와 같이 다음 44827를 대입해서 다음으로 넘어가라는 코드가 있다.
이는 계속 반복되고 끝날때 까지 돌아갈 것이다.
문제는 400번 이상의 횟수를 반복해야하기 때문에 코드로 작성해야한다는 것이다.
파이썬 코드로 아래를 작성해 보았다.
from urllib.request import urlopen
import re
def url_read(url):
url_data = urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='+str(url)).read().decode('utf-8')
next_url = re.sub(r'[^0-9]', '', url_data)
return next_url
url = 12345
while True:
try:
url_end = url
url = url_read(url)
print(url)
if str(url) == "":
print('end')
print(url_end)
print(urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='+str(url_end)).read().decode('utf-8'))
break
except:
print(url)
작동하다가 멈춰서 내용을 보니 아래와 같은 문구가 떠서 해당 url을 다시 반으로 나누고 코드를 실행하니
다음 Level 5 url