편집 기록

편집 기록
  • 프로필 엽토군님의 편집
    날짜2022.10.14

    python 크롤링 문의


    안녕하세요. 파이썬을 이용해서 크롤링을 공부하고 있는 초보자입니다!

    request와 Beautiful함수를 이용해서 url에 접속해서 크롤링을하고 있습니다.

    대부분의 경우에서 검색을 통해서 찾은 방법으로 정보를 크롤링을 할 수 있었지만, 현재 일부 사이트에서 크롤링이 막혀서 전문가분들께 문의드립니다.

    https://www.accu.co.uk/8-cap-head-screws?page=1

    라는 페이지에서 url링크를 따오고자 하고있는데요.

    id="product-grid">
    
        class="container mt-5">    
    
                ...
    
            a x-bind:href="product[currentIso].uri" x-bind:title="product[currentIso].title" href="/cap-head-screws/3819-SSCF-M3-10-A2" title="M3 x 10mm Full Thread Cap Head Screws (DIN 912) - Stainless Steel (A2)">
    </a>
    

    위와같이 "product-grid"라는 id내에 있는 class "container"내에 있는 anchor의 href 주소를 가져오고자 하고있습니다.

    아래와 같이 python으로 코드를 짜서 print를 해보았는데요.

    from bs4 import BeautifulSoup
    
    from urllib.request import Request, urlopen
    
    req = Request('https://www.accu.co.uk/8-cap-head-screws?page=1',
    
                    headers={'User-Agent': 'Mozilla/5.0'})
    
    webpage = urlopen(req).read()
    
    soup = BeautifulSoup(webpage, "html.parser")
    
    categories = soup.select('#product-grid .container a')
    
    print(categories)
    
    

    결과는 아래와같습니다..

    a x-bind:href="product[currentIso].uri" x-bind:title="product[currentIso].title">
    div :class="product.image_transparent ? 'bg-gray-100' : 'border-b'" class="overflow-hidden rounded-tl-md rounded-tr-md relative">
    div class="aspect-w-1 aspect-h-1">
    img class="Sirv p-8" src="https://i.accu.co.uk/placeholder.svg" x-bind:alt="product[currentIso].title" x-bind:src="product.image" x-bind:title="product[currentIso].title"/>
    

    위와같이 a x-bind: ~~~~ .title">까지만 출력되고 그이후 제가 원하는 href인 /cap-head-screw/... 는 출력이 되지 않습니다.

    이런식으로 되어있는 구조말고는 출력이 잘되었었는데, 여기서는 왜안되는지 이유를 잘모르겠네요..

    고수분들있으시면 조언부탁드리겠습니다.

    감사합니다!

  • 프로필 hdh0914.1@gmail.com님의 편집
    날짜2022.10.13

    python 크롤링 문의


    안녕하세요. 파이썬을 이용해서 크롤링을 공부하고 있는 초보자입니다!

    request와 Beautiful함수를 이용해서 url에 접속해서 크롤링을하고 있습니다.

    대부분의 경우에서 검색을 통해서 찾은 방법으로 정보를 크롤링을 할 수 있었지만, 현재 일부 사이트에서 크롤링이 막혀서 전문가분들께 문의드립니다.

    https://www.accu.co.uk/8-cap-head-screws?page=1

    라는 페이지에서 url링크를 따오고자 하고있는데요.

    id="product-grid">

    class="container mt-5">    
    
            ...
    
        a x-bind:href="product[currentIso].uri" x-bind:title="product[currentIso].title" href="/cap-head-screws/3819-SSCF-M3-10-A2" title="M3 x 10mm Full Thread Cap Head Screws (DIN 912) - Stainless Steel (A2)">
    

    위와같이 "product-grid"라는 id내에 있는 class "container"내에 있는 anchor의 href 주소를 가져오고자 하고있습니다.

    아래와 같이 python으로 코드를 짜서 print를 해보았는데요.

    ################# Code

    from bs4 import BeautifulSoup

    from urllib.request import Request, urlopen

    req = Request('https://www.accu.co.uk/8-cap-head-screws?page=1',

                headers={'User-Agent': 'Mozilla/5.0'})
    

    webpage = urlopen(req).read()

    soup = BeautifulSoup(webpage, "html.parser")

    categories = soup.select('#product-grid .container a')

    print(categories)

    결과는 아래와같습니다..

    a x-bind:href="product[currentIso].uri" x-bind:title="product[currentIso].title"> div :class="product.image_transparent ? 'bg-gray-100' : 'border-b'" class="overflow-hidden rounded-tl-md rounded-tr-md relative"> div class="aspect-w-1 aspect-h-1"> img class="Sirv p-8" src="https://i.accu.co.uk/placeholder.svg" x-bind:alt="product[currentIso].title" x-bind:src="product.image" x-bind:title="product[currentIso].title"/>

    위와같이 a x-bind: ~~~~ .title">까지만 출력되고 그이후 제가 원하는 href인 /cap-head-screw/... 는 출력이 되지 않습니다.

    이런식으로 되어있는 구조말고는 출력이 잘되었었는데, 여기서는 왜안되는지 이유를 잘모르겠네요..

    고수분들있으시면 조언부탁드리겠습니다.

    감사합니다!