I want to use the input function. Help me

Asked 2 years ago, Updated 2 years ago, 70 views

import requests
from bs4 import BeautifulSoup as bs

marketType = {"KOSPI": "0", "KOSDAQ": "1"}
for market, code in marketType.items():
    for page in range(1, 36):
        # To request data
        req = requests.get(f"https://finance.naver.com/sise/sise_market_sum.naver?sosok={code}&page={page}")
        html = req.text
        soup = bs(html, "lxml")


        # Extracting data
        StockContents = soup.select("#contentarea > div.box_type_l > table.type_2 > tbody > tr")
        for StockContent in StockContents:
            try:
                StockRank = StockContent.select_one("td.no").text
                StockName = StockContent.select_one("td:nth-child(2) > a").text
                StockPrice = StockContent.select_one("td:nth-child(3)").text
                StockCap = StockContent.select_one("td:nth-child(7)").text
                print(The current price of {StockName} stocks such as f"{market}{StockRank} is KRW {StockPrice} and the market capitalization is KRW {StockCap} billion.)

            except AttributeError:
                continue

Among the codes above, req = requests.get followed by {code} is the part that distinguishes between KOSPI and KOSDAQ, and those are printed at the same time. If you run them, in order... But I want the user to distinguish 0 and 1 as input and print one of the KOSPI KOSDAQ, so what should I do? I've already finished coding, so I don't know where to fix it with my skills

input pycharm

2022-09-20 14:44

1 Answers

I can make any number of suggestions, but I'll just add two lines first.

marketType = {"KOSPI": "0", "KOSDAQ": "1"}
codeInput = input (for entering 0 or 1)
for market, code in marketType.items():
    if code is codeInput :
        for page in range(1, 36) :
            # The following is omitted


2022-09-20 14:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.