<jupyter_start><jupyter_text>Use the requests library to download the web pages<jupyter_code>import requests

url = "https://coinmarketcap.com/"
response = requests.get(url)
response.status_code
len(response.text)
page_contents = response.text
page_contents[:1000]
with open ("webpage.html","w") as f:
    f.write(page_contents)<jupyter_output><empty_output><jupyter_text>Use BeautifulSoup to parse and extract information<jupyter_code>from bs4 import BeautifulSoup
doc = BeautifulSoup(page_contents,"html.parser")
ticker_tags = doc.find_all("p",class_ = "sc-1eb5slv-0 iworPT")
 len(ticker_tags)
ticker_tags[0].text
tick_price_tags = doc.find_all("div",{"class":"sc-131di3y-0 cLgOOr"})
len(tick_price_tags)
tick_price_tags[0].text
market_cap = doc.find_all("span",class_ = "sc-1ow4cwt-0 iosgXe")
len(market_cap)
market_cap[0].text
volume_tag = doc.find_all("p",class_ = "sc-1eb5slv-0 hykWbK font_weight_500")
len(volume_tag)
volume_tag[0]
topic_titles = []
for tag in ticker_tags:
    topic_titles.append(tag.text)
print(topic_titles)
topic_price = []

for tag in tick_price_tags:
    topic_price.append(tag.text)
print(topic_price)
market_capital=[]
for tag in market_cap:
    market_capital.append(tag.text)
print(market_capital)
volume_tags = []
for tag in volume_tag:
    volume_tags.append(tag.text)
print(volume_tags)
import pandas as pd
a = {
    "title": topic_titles,
    "price": topic_price,
    "capital": market_capital,
    "volume" : volume_tags
}
df = pd.DataFrame.from_dict(a, orient = 'index')
df = df.transpose()
df<jupyter_output><empty_output><jupyter_text>Create a csv file(s) with the extracted information<jupyter_code>df.to_csv("coinmarketcap.csv",index = None)<jupyter_output><empty_output><jupyter_text>write a single function to  :1. get the list of coins from the page2. get the information from the page 3. create a csv file of the info<jupyter_code>def topics_title(doc):
    ticker_tags = doc.find_all("p",class_ = "sc-1eb5slv-0 iworPT")
    topic_titles = []
    for tag in ticker_tags:
        topic_titles.append(tag.text)
    print(topic_titles)
    return topic_titles

def topic_price(doc):
    tick_price_tags = doc.find_all("div",{"class":"sc-131di3y-0 cLgOOr"})
    topic_price = []
    for tag in tick_price_tags:
        topic_price.append(tag.text)
    print(topic_price)
    return topic_price


def topic_capital(doc):
    market_cap = doc.find_all("span",class_ = "sc-1ow4cwt-0 iosgXe")
    market_capital=[]
    for tag in market_cap:
        market_capital.append(tag.text)
    print(market_capital)
    return market_capital

def topic_volume(doc):
    volume_tag = doc.find_all("p",class_ = "sc-1eb5slv-0 hykWbK font_weight_500")
    volume_tags = []
    for tag in volume_tag:
        volume_tags.append(tag.text)
    print(volume_tags)
    return volume_tags


def scrape_info():
    url = "https://coinmarketcap.com/"
    response = requests.get(url)
    if response.status_code != 200:
        raise Exception('Failed to load page{}'.format(url))
    topic_dict = {
         "title": topics_title(doc),
         "price": topic_price(doc),
         "capital": topic_capital(doc),
         "volume" : topic_volume(doc)  
    }
    return pd.DataFrame.from_dict(a, orient = 'index')
scrape_info()

for index ,row in df.iterrows():
    print(row[0],row[1])<jupyter_output>ShibaDoge $0.02846
Secured Ship $0.000000000912
Baby Floki Billionaire $1.49
Star Wars Cat $41,383.20
ASIX Token $2,<PASSWORD>
Talent Token $1.00
ASIMI $393.94
Pudgy Pups Club $1
Gateway Protocol $0.7375
Bitcoin $92.68
Ethereum $91.65
Tether $0.8673
BNB $77.25
USD Coin None
XRP None
Terra None
Solana None
Cardano None
Avalanche None
{'fraction_non_alphanumeric': 0.11922340716434236, 'fraction_numerical': 0.030626196335794366, 'mean_word_length': 4.662538699690402, 'pattern_counts': {'":': 8, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '545494', 'license': 'MIT', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1346, 'n_words': 284, 'path': 'webscraping on coinmarketcap.ipynb', 'repo_name': 'shyamsuthar/webscraping-coinmarketcap', 'size': '3653'}