# CoinMarketCap'ten Exchange Bilgilerini Çekme ve CSV'ye Kaydetme

## 🔰 Projeye Bakış

{% hint style="info" %}
Yüksek kaliteli hali **8 saat içerisinde** işlenecektir... 4 May 2023 16:54:41
{% endhint %}

{% embed url="<https://youtu.be/Svr6cMbAOEA>" %}

<figure><img src="/files/QozEyxDJ1p2ssS39WBEC" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/Vnb7C2LHxFUZQW2iX7t5" alt="" width="563"><figcaption></figcaption></figure>

## 👨‍💻 Kaynak Kodu

```python
from seleniumwire.webdriver import Chrome
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By
from time import sleep
from typing import Any
import csv

MAX_ROW = 60
MAX_ROW_ENLARGED = 200

TABLE_XPATH = '//*[@id="__next"]/div/div[1]/div[2]/div/div/div[2]/table'
BUTTON_XPATH = '//*[@id="__next"]/div/div[1]/div[2]/div/div/div[3]/button'


def scroll_into_elem(elem: WebElement):
    chrome.execute_script("arguments[0].scrollIntoView();", elem)
    sleep(0.5)


def write_to_csv(filename: str, data: list[list[Any]]):
    with open(filename, "w", newline="") as csvfile:
        csvwriter = csv.writer(csvfile)
        csvwriter.writerow(
            ["index", "exchange", "score", "volume", "liqudity", "markets", "coins"]
        )
        for row in data:
            csvwriter.writerow(row)


def fetch_table_rows(
    elem: WebElement, first_row: int, last_row: int
) -> list[list[Any]]:
    """Fetches the table rows from the given element"""
    fetched_rows: list[list[Any]] = []
    for i in range(first_row, last_row + 1):
        row_elem = elem.find_element(By.XPATH, f"tbody/tr[{i}]")
        splitted_row = row_elem.text.split("\n")
        if len(splitted_row) < 9:
            scroll_into_elem(row_elem)
            row_elem = elem.find_element(By.XPATH, f"tbody/tr[{i}]")
        [
            index,
            exchange,
            score,
            volume,
            _,
            liqudity,
            _,
            markets_coins,
        ] = row_elem.text.split("\n")[:8]
        [markets, coins] = markets_coins.split(" ")
        index, score, volume, liqudity, markets, coins = (
            int(index),
            float(score),
            float(volume.replace("$", "").replace(",", "")),
            int(liqudity) if liqudity != "--" else None,
            int(markets),
            int(coins),
        )
        fetched_rows.append([index, exchange, score, volume, liqudity, markets, coins])
    return fetched_rows


if __name__ == "__main__":
    chrome = Chrome()
    chrome.get("https://coinmarketcap.com/rankings/exchanges/")

    elem = chrome.find_element(By.XPATH, TABLE_XPATH)
    rows = fetch_table_rows(elem, 1, MAX_ROW)
    button = chrome.find_element(By.XPATH, BUTTON_XPATH)
    button.click()
    sleep(2)
    rows.extend(fetch_table_rows(elem, MAX_ROW + 1, MAX_ROW_ENLARGED))
    write_to_csv("data.csv", rows)

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.yemreak.com/arsiv/yazilarim/coinmarketcapten-exchange-bilgilerini-cekme-ve-csvye-kaydetme.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
