Home crawling-task-3
Post
Cancel

crawling-task-3

주소가 연결되는지 확인

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import requests

def checking():
  print('Welcome to IsItDown.py!')
  urls = list(map(str, input('Please write a URL or URLs you want to check.( separated by comma)\n').split(',')))
  
  for i in urls:
    if '.com' in i:
      if 'http' not in i:
        url = 'https://' + i.strip().lower()
      else:
        url = i.strip().lower()
      
      try:
        r = requests.get(url).status_code
      except :
        r = 404

      if r == 200:
        print(url + ' is up!')
      else:
        print(url + ' is down!')
      again()
    else:
      print(i.strip().lower() + ' is not a valid URL.')

def again():
  ans = input('Do you want to start over? y/n ')
  if ans == 'y':
    checking()
  elif ans == 'n':
    return
  else:
    print('That`s not a valid answer')
    again()

checking()

메모

  • 해답으로 올라온 코드는 이하와 같다
  • 기본 개념은 잘 이해한 것 같으나
    작성하는 습관은 잘 익혀야 할 것 같다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import requests

def restart():
  answer = str(input("Do you want to start over? y/n ")).lower()
  if answer == "y" or answer =="n":
    if answer == "n":
        print("k. bye!")
        return
    elif answer == "y":
      main()
  else:
    print("That's not a valid answer")
    restart()

def main():
  os.system('clear')
  print("Welcome to IsItDown.py!\nPlease write a URL or URLs you want to check. (separated by comma)")
  urls = str(input()).lower().split(",")
  for url in urls:
    url = url.strip()
    if "." not in url:
      print(url, "is not a valid URL.")
    else:
      if "http" not in url:
        url = f"http://{url}"
      try:
        request = requests.get(url)
        if request.status_code == 200:
          print(url,"is up!")
        else:
          print(url, "is down!")
      except:
          print(url, "is down!")
  restart()

main()

This post is licensed under CC BY 4.0 by the author.