<jupyter_start><jupyter_text>Текст<jupyter_code>!pip install beautifulsoup4
import requests
from bs4 import BeautifulSoup
url = "https://knowyourmeme.com/"
requests.get(url)
url = "http://books.toscrape.com/catalogue/page-3.html666"
requests.get(url)
url = "http://books.toscrape.com/catalogue/page-3.html"
res = requests.get(url)
res
soup = BeautifulSoup(res.content)
soup.title.text
item = soup.find("article", {"class": "product_pod"})
item
type(item)
item.h3.a
item.h3.a.text
item.h3.a.get('href')
item.h3.a.get('title')
items = soup.find_all("article", {"class": "product_pod"})
[item.h3.a.get('href') for item in items]
def get_hrefs(p):
    url = f"http://books.toscrape.com/catalogue/page-{p}.html"
    res = requests.get(url)
    soup = BeautifulSoup(res.content)
    items = soup.find_all("article", {"class": "product_pod"})
    hrefs = [item.h3.a.get('href') for item in items]
    titles = [item.h3.a.get('title') for item in items]
    return titles, hrefs
from tqdm.notebook import tqdm 

titles = [ ]
hrefs = [ ]
for p in tqdm(range(1, 51)):
    t,h = get_hrefs(p)
    titles.extend(t)
    hrefs.extend(h)
import pandas as pd 
df = pd.DataFrame({'title': titles, 'href': hrefs})
df.head()
df.to_csv('hrefs.csv', sep='\t')
def get_info(href):
    main_url = "http://books.toscrape.com/catalogue/"
    main_url += href
    
    res = requests.get(main_url)
    soup = BeautifulSoup(res.content)
    
    # desc = soup.find('article').find_all('p')[-1].text
    price = soup.find('p', {"class" :"price_color"}).text
    
    info = {
        'href': href, 
        'describe': desc, 
        'price': price
    }
    return info
pd.read_html("http://books.toscrape.com/catalogue/sleeping-giants-themis-files-1_604/index.html")
pd.read_html(str(soup.find('table')))[0]
info = [ ]
for url in df.href.values[:10]:
    info.append(get_info(url))
df = pd.DataFrame(info)
df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18039624608967675, 'fraction_numerical': 0.013034410844629822, 'mean_word_length': 3.895408163265306, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '342335', 'license': 'MIT', 'n_tokens_mistral': 771, 'n_tokens_neox': 748, 'n_words': 132, 'path': 'sem12_visual/sem11_201&204.ipynb', 'repo_name': 'hse-econ-data-science/dap_2021_spring', 'size': '1918'}