TIL_Code lion_python(3)

박상욱·2022년 2월 25일
0

실시간 검색어 확인하기

from bs4 import BeautifulSoup
import requests
from datetime import datetime

url = "http://www.daum.net/"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
rank = 1

results = soup.findAll('a','link_favorsch')

search_rank_file = open("rankresult.txt","a")

print(datetime.today().strftime("%Y년 %m월 %d일의 실시간 검색어 순위입니다.\n"))

for result in results:
    search_rank_file.write(str(rank)+"위:"+result.get_text()+"\n")
    print(rank,"위 : ",result.get_text(),"\n")
    rank += 1

함수

모듈

리퀘스트 모듈

#순서


request.get은

BeautifulSoup


데이터를 파싱(뭉쳐져있는 문자열을 의미있는 값으로 분해)하는 beautifulsoup 함수

데이터에는 HTML,XML이 올 수 있다.

BeautifulSoup으로 예쁘게 출력하기 (크롤링)

from bs4 import BeautifulSoup
import requests
from datetime import datetime

url = "http://www.naver.com/"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

search_issue_file = open("naver.text","w")
file.write(response.text)
file.close()

results = soup.findAll('a','issue')
rank = 0

for result in results:
    rank = rank + 1
    search_issue_file.write(str(rank)+'위'+result.string+'\n')
    print(rank,'위 ',result.string,"\n")

print(datetime.today().strftime("%Y년 %m월 %d일의 실시간 이슈입니다."))

open 함수

open(파일(읽거나 쓸 대상이 되는 파일) ,모드(읽거나 쓸 대상이 될 파일의 상태)[r,w,a]

나는 로봇이 아니야

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}

response = requests.get(url,headers=headers)

profile
기획,디자이너,개발 찍먹파입니다.

0개의 댓글