import requests
url = "<URL>" # 예제 URL
response = requests.get(url)
print(response.status_code) # 상태코드 출력
url = "https://search.naver.com/search.naver?query=서울날씨"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
feel_temp = soup.select_one('#main_pack > section.sc_new.cs_weather_new._cs_weather > div > div:nth-child(1) > div.content_wrap > div.open > div:nth-child(1) > div > div.weather_info > div > div._today > div.temperature_info > dl > div:nth-child(1) > dd').get_text()
humidity = soup.select_one('#main_pack > section.sc_new.cs_weather_new._cs_weather > div > div:nth-child(1) > div.content_wrap > div.open > div:nth-child(1) > div > div.weather_info > div > div._today > div.temperature_info > dl > div:nth-child(2) > dd').get_text()
wind = soup.select_one('#main_pack > section.sc_new.cs_weather_new._cs_weather > div > div:nth-child(1) > div.content_wrap > div.open > div:nth-child(1) > div > div.weather_info > div > div._today > div.temperature_info > dl > div:nth-child(3) > dd').get_text()
print("체감온도:", feel_temp)
print("습도:", humidity)
print("서풍:", wind)
import pandas as pd
# 데이터 준비
data = [{'temperature': feel_temp, 'humidity': humidity, 'wind': wind}]
# DataFrame 생성 및 CSV 파일로 저장
df = pd.DataFrame(data)
df.to_csv('weather_webscraping.csv', index=False)
print("날씨 정보가 CSV 파일로 저장되었습니다.")
# 클래스 정의
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"안녕하세요, 저는 {self.name}이고, 나이는 {self.age}살입니다.")
def __str__(self):
return f"!학생에 대한 정보입니다!\n이름: {self.name}\n나이: {self.age}"
# 객체1, 홍길동 생성
student1 = Student("홍길동", 20)
student1.introduce()
print(student1)
# 객체2, 이영희 생성
student2 = Student("이영희", 22)
student2.introduce()
print(student2)
Q. 네이버 경제 > 증권 뉴스에 보이는 뉴스 제목을 출력해주세요!
# 라이브러리
import requests
from bs4 import BeautifulSoup
# url 가져오기
url = "https://news.naver.com/breakingnews/section/101/258"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 타이틀 가져오기
titles =soup.findAll('strong', {'class' : 'sa_text_strong'})
for title in titles:
print(title.get_text())