강의

멘토링

커뮤니티

Inflearn Community Q&A

dypic's profile image
dypic

asked

[New Revised Edition] This is Real Web Crawling - Basic Edition

질문 드립니다..

Written on

·

269

1

import requests
from bs4 import BeautifulSoup
import openpyxl

fpath = r"C:\Users\OWNER\Desktop\python\주.xlsx"
wb = openpyxl.load_workbook(fpath)
ws = wb.active

codes = [
    '005930',
    '000660',
    '035720'
]

row = 2
for code in codes :
    url = f"https://finance.naver.com/item/sise.naver?code={codes}"
    response = requests.get(url)
    html = response.text
    soup = BeautifulSoup(html,'html.parser')
    price = soup.select_one('#_nowVal').text
    price = price.replace(',','')
    print(price)
    ws[f'B{row}'] = int(price)
    row = row + 1
   
   
wb.save(fpath)

어떤오류인지 알 수가 없어서요..

 

 

python웹-크롤링

Answer 2

0

startcoding님의 프로필 이미지
startcoding
Instructor

안녕하세요.

 

코딩을 가장 쉽게 알려주는 크리에이터 스타트코딩입니다. 

 

오타가 하나 있습니다.

url = f"https://finance.naver.com/item/sise.naver?code={codes}"

리스트 변수인 codes 대신,

리스트의 원소 하나를 나타내는 code 로 바꿔 주셔야 합니다. 

 

 

감사합니다. 

좋은 하루 되세요 ^^

- 스타트코딩 드림.

0

dypic님의 프로필 이미지
dypic
Questioner

 

Traceback (most recent call last):

  File "c:\Users\OWNER\Desktop\python\ex_naverfinance.py", line 24, in <module>

    ws[f'B{row}'] = int(price)

ValueError: invalid literal for int() with base 10: ''

dypic's profile image
dypic

asked

Ask a question