Python으로 Bing 관련 검색어 긁기

내용: 인트로, 가져오기, 스크랩할 항목, 프로세스, 코드, 링크, 아웃트로.

소개



이 블로그 게시물에는 Python을 사용하여 Bing 관련 검색을 스크랩하는 방법에 대한 시각적 정보가 포함되어 있습니다.

수입품




from bs4 import BeautifulSoup
import requests
import lxml
from serpapi import GoogleSearch
import os # used for creating an environment variable on replit.com


스크랩 할 것





프로세스



올바른 CSS 선택기 선택 두 가지 접근 방식을 보여 드리겠습니다.
  • Chrome Dev 도구를 사용하여 확장자가 SelectorGadget인 HTML 레이아웃 및 테스트 선택기 보기
  • SelectorGadget 확장만 사용

  • 거기에서 두 가지 접근 방식의 선택기 중 하나가 작동합니다.



    암호




    from bs4 import BeautifulSoup
    import requests, lxml
    
    headers = {
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
    }
    
    html = requests.get('https://www.bing.com/search?q=lion king&hl=en', headers=headers)
    soup = BeautifulSoup(html.content, 'lxml')
    
    for related_search in soup.select('.b_rs ul li'):
        title = related_search.text
        link = f"https://www.bing.com{related_search.a['href']}"
        print(f'{title}\n{link}')
    
    # part of the output:
    '''
    lion
    https://www.bing.com/search?q=lion&FORM=QSRE1
    jeremy irons
    https://www.bing.com/search?q=jeremy+irons&FORM=QSRE2
    '''
    


    Bing 관련 검색 API 사용



    SerpApi는 5,000회 검색의 무료 평가판이 있는 유료 API입니다.
    결과를 얻는 방법은 다음과 같습니다.



    from serpapi import GoogleSearch
    import  os
    
    params = {
      "api_key": os.environ["API_KEY"], # pycharm environment
      "engine": "bing",
      "q": "lion king"
    }
    
    search = GoogleSearch(params)
    results = search.get_dict()
    
    for result in results['related_searches']:
        query = result['query']
        link = result['link']
        print(f'{query}\n{link}')
    
    # part of the output:
    '''
    elton john circle of life
    https://www.bing.com/search?q=elton+john+circle+of+life&FORM=QSRE1
    lion king theatre
    https://www.bing.com/search?q=lion+king+theatre&FORM=QSRE2
    '''
    


    연결



    Code in the online IDE • Bing Related Searches API

    아웃트로



    질문이 있거나 제대로 작동하지 않거나 다른 내용을 작성하고 싶은 경우 댓글 섹션이나 Twitter(으)로 자유롭게 의견을 남겨주세요.

    당신 것,
    Dimitry 및 나머지 SerpApi 팀.

    좋은 웹페이지 즐겨찾기