Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Tags more
Archives
Today
Total
관리 메뉴

맨땅에 코딩

텍스트 크롤링 (BeautifulSoup 패키지 사용) 본문

AI/크롤링

텍스트 크롤링 (BeautifulSoup 패키지 사용)

맨땅 2023. 2. 15. 00:19

목차

    반응형

     

    ● Beautiful Soup 패키지란 ?

     

    출처 : 위키백과

    https://ko.wikipedia.org/wiki/%EB%B7%B0%ED%8B%B0%ED%92%80_%EC%88%98%ED%94%84_(HTML_%ED%8C%8C%EC%84%9C)

     

     


     

     

    #!/usr/bin/env python3
    # Anchor extraction from HTML document
    from bs4 import BeautifulSoup
    from urllib.request import urlopen
    
    response = urlopen('https://en.wikipedia.org/wiki/Main_Page')
    soup = BeautifulSoup(response, 'html.parser')
    f = open("새파일.txt", 'w')
    i = 1
    
    for anchor in soup.find_all('a'):
        print(anchor.get('href', '/'))
        data = str(i) + "번째 : " + anchor.get('href', '/') + "\n"
        i = i + 1
        f.write(data)
    f.close();

     

     

     

     

     

    실행 명령어 : python index.py

     

     

     

     

     

     

     

    반응형