Python을 사용하여 Bing 뉴스 긁기
8442 단어 webdevwebscrapingpythontutorial
소개
글쎄, 마지막 Bing 시리즈에서 온 사람들에게 안녕하세요! 이 블로그 게시물은 Bing의 웹 스크래핑 시리즈의 연속이며 Python을 사용하여 Bing 뉴스 결과를 스크래핑하는 방법에 대한 정보를 포함합니다. 첫 번째 코드 블록 다음에 대체 솔루션이 표시됩니다.
수입품
import requests
import lxml
from bs4 import BeautifulSoup
from serpapi import GoogleSearch
스크랩 할 것
프로세스
프로세스는 간단합니다. SelectorGadget Chrome 확장 프로그램이
CSS
선택기를 가져왔습니다.다음 GIF는 제목, URL, 스니펫, 소스 웹 사이트의 선택기
CSS
를 가져오는 방법과 뉴스가 게시된 시기를 보여줍니다.암호
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/news/search?q=faze+clan', headers=headers)
soup = BeautifulSoup(html.text, 'lxml')
for result in soup.select('.card-with-cluster'):
title = result.select_one('.title').text
link = result.select_one('.title')['href']
snippet = result.select_one('.snippet').text
source = result.select_one('.source a').text
date_posted = result.select_one('#algocore span+ span').text
print(f'{title}\n{link}\n{source}\n{date_posted}\n{snippet}\n')
# part of the output:
'''
FaZe Clan shows off new execute for Mirage against Furia Esports
https://win.gg/news/8521/faze-clan-shows-off-new-execute-for-mirage-against-furia-esports
WIN.gg
2h
During a match against Team Furia in the Gamers Without Borders Cup, the camera spotted an interesting interaction between ...
'''
Bing 뉴스 엔진 결과 API 사용
SerpApi는 5,000회 검색의 무료 평가판이 있는 유료 API입니다.
from serpapi import GoogleSearch
import json
params = {
"api_key": "YOUR_API_KEY",
"engine": "bing_news",
"q": "faze clan"
}
search = GoogleSearch(params)
results = search.get_dict()
for result in results['organic_results']:
print(json.dumps(result, indent=2, ensure_ascii=False))
# part of the output:
'''
{
"title": "FaZe Clan shows off new execute for Mirage against Furia Esports",
"link": "https://win.gg/news/8521/faze-clan-shows-off-new-execute-for-mirage-against-furia-esports",
"snippet": "During a match against Team Furia in the Gamers Without Borders Cup, the camera spotted an interesting interaction between ...",
"source": "WIN.gg",
"date": "2h",
"thumbnail": "https://serpapi.com/searches/60d82f308ccee022b4ab7525/images/62e054f4209c882415dd75f5245f96d23bd4c1538d707fb513a0918671c831d7.jpeg"
}
'''
링크
Code in the online IDE • Bing News Engine Results API
아웃트로
질문이 있거나 제대로 작동하지 않거나 다른 내용을 작성하고 싶은 경우 댓글 섹션이나 Twitter(으)로 자유롭게 의견을 남겨주세요.
당신 것,
Dimitry 및 나머지 SerpApi 팀.
Reference
이 문제에 관하여(Python을 사용하여 Bing 뉴스 긁기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/dmitryzub/scrape-bing-news-using-python-167f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)