참고용, AI가 짜준 파이썬 데이타 크롤링 프로그램

Updated on 2023-12-03 by

AI에게 다음과 같이 질문을 해서 답을 받는 내용을 기록해 봅니다. 아직 파이썬을 배우지 않아서 이 프로그램이 제대로 작동하는지는 모릅니다.

https://companiesmarketcap.com/ 사이트 데이타를 한국 시간 기준 매일 오전 7시에 정기적으로 백업할 수 있도록 프로그램을 짜줄 수 있겠니?

AI가 짜준 파이썬 데이타 크롤링 프로그램

이 프로그램은 한국 시간 기준 매일 오전 7시에 https://companiesmarketcap.com/ 사이트의 데이터를 스크랩하고 companies_market_cap.csv 파일에 저장합니다. time_string 변수를 원하는 시간으로 변경할 수 있습니다. 예를 들어, time_string = “12:00” 으로 설정하면 정오에 데이터를 백업합니다.

이 프로그램을 실행하려면 requests, BeautifulSoup, schedule 라이브러리가 필요합니다.

이 프로그램은 참고용이며, 실제로 사용하기 전에 테스트하고 필요한 경우 수정하시기 바랍니다.

https://companiesmarketcap.com/ 사이트의 데이터를 한국 시간 기준 매일 오전 7시에 정기적으로 백업할 수 있는 파이썬 프로그램을 작성해 보았습니다. 다음은 코드입니다.

코드

# Import requests, BeautifulSoup, schedule and time libraries
import requests
from bs4 import BeautifulSoup
import schedule
import time

# Define the URL of the website
url = "https://companiesmarketcap.com/"

# Define a function to scrape the data from the website
def scrape_data(url):
    # Send a GET request to the website and get the response
    response = requests.get(url)
    # Check if the response status code is 200 (OK)
    if response.status_code == 200:
        # Parse the response content using BeautifulSoup
        soup = BeautifulSoup(response.content, "html.parser")
        # Find the table element that contains the data
        table = soup.find("table", id="companies")
        # Find all the table rows that contain the data
        rows = table.find_all("tr")
        # Create an empty list to store the data
        data = []
        # Loop through each row
        for row in rows:
            # Find all the table cells that contain the data
            cells = row.find_all("td")
            # Check if the row has 7 cells
            if len(cells) == 7:
                # Extract the data from each cell
                rank = cells[0].text.strip()
                name = cells[1].text.strip()
                market_cap = cells[2].text.strip()
                price = cells[3].text.strip()
                today = cells[4].text.strip()
                volume = cells[5].text.strip()
                ratio = cells[6].text.strip()
                # Create a dictionary to store the data for each company
                company = {
                    "rank": rank,
                    "name": name,
                    "market_cap": market_cap,
                    "price": price,
                    "today": today,
                    "volume": volume,
                    "ratio": ratio
                }
                # Append the dictionary to the data list
                data.append(company)
        # Return the data list
        return data
    else:
        # Return None if the response status code is not 200
        return None

# Define a function to save the data to a CSV file
def save_data(data, filename):
    # Import the csv library
    import csv
    # Open the file in write mode
    with open(filename, "w") as file:
        # Create a csv writer object
        writer = csv.writer(file)
        # Write the header row
        writer.writerow(["Rank", "Name", "Market Cap", "Price", "Today", "Volume", "P/E Ratio"])
        # Loop through each company in the data list
        for company in data:
            # Write the data row
            writer.writerow([company["rank"], company["name"], company["market_cap"], company["price"], company["today"], company["volume"], company["ratio"]])

# Define a function to schedule the scraping and saving tasks
def schedule_tasks(url, filename, time_string):
    # Define a task to scrape and save the data
    def task():
        # Print a message
        print(f"Scraping and saving data from {url} at {time.ctime()}")
        # Scrape the data from the website
        data = scrape_data(url)
        # Check if the data is not None
        if data:
            # Save the data to the file
            save_data(data, filename)
            # Print a message
            print(f"Data saved to {filename}")
        else:
            # Print a message
            print(f"Failed to scrape data from {url}")
    # Schedule the task to run at the specified time every day
    schedule.every().day.at(time_string).do(task)
    # Loop indefinitely
    while True:
        # Run the pending tasks
        schedule.run_pending()
        # Sleep for 1 second
        time.sleep(1)

# Define the main function
def main():
    # Define the URL of the website
    url = "https://companiesmarketcap.com/"
    # Define the filename of the CSV file
    filename = "companies_market_cap.csv"
    # Define the time string in HH:MM format
    time_string = "07:00" # 7 AM in Korean Standard Time
    # Schedule the scraping and saving tasks
    schedule_tasks(url, filename, time_string)

# Call the main function
if __name__ == "__main__":
    main()
Code language: PHP (php)

새롭게 뉴스레터를 시작했습니다.

1️⃣ 주식 등 투자 정보 : 기업 분석, IB 투자의견 등 투자 관련 내용
..... 테슬라 실적 및 IB들의의 테슬라 투자의견
2️⃣ 사례 및 트렌드 : 사례연구와 트렌드 관련 괜찮은 내용
.....유튜브와 경쟁대신 구독 전환한 비디오 플래폼 비메오 사례

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments