私はこれまでインターネットからのデータの解析を扱う必要はありませんでした。通常、作業用のすべてのデータ(データアナリスト)は、単純な内部インターフェイスを使用した会社のアンロードから取得されるか、「以前の収益を確認する」よりも複雑なものが必要な場合は、ストレージから直接テーブルへのSQLクエリによって形成されます。月"。
したがって、サードパーティのツールを使用せずに便利なIDEのコードを使用してインターネットからデータを収集できるようにするために、HTMLページを解析するための簡単なツールをマスターしたいと思いました。
データを収集するサイトは、「パーサーブロッカーがない」と「このデータの分析から何か面白いことが出てくるかもしれない」という原則に基づいて選択されました。そのため、サンクトペテルブルクで「東京シティ」、「ユーラシア」、「2ベレガ」の3つのレストランを提供するための料理の品揃えが選択されました。彼らはほぼ同じ料理の焦点と同様の品揃えを持っているので、明らかに比較するものがあります。
パーサー自体をレストランの1つで共有します。
import requests
from bs4 import BeautifulSoup
import pandas as pd
import datetime
print(" : " + str(datetime.datetime.now()))
#
urllist = ['https://www.tokyo-city.ru/spisok-product/goryachie-blyuda1.html',
'https://www.tokyo-city.ru/spisok-product/sushi.html',
'https://www.tokyo-city.ru/spisok-product/rolly.html',
'https://www.tokyo-city.ru/spisok-product/nabory.html',
'https://www.tokyo-city.ru/spisok-product/new_lunches.html',
'https://www.tokyo-city.ru/spisok-product/pitctca.html',
'https://www.tokyo-city.ru/spisok-product/salaty.html',
'https://www.tokyo-city.ru/spisok-product/-supy-.html',
'https://www.tokyo-city.ru/spisok-product/goryachie-zakuski1.html',
'https://www.tokyo-city.ru/spisok-product/wok.html',
'https://www.tokyo-city.ru/spisok-product/pasta.html',
'https://www.tokyo-city.ru/spisok-product/gamburgery-i-shaverma.html',
'https://www.tokyo-city.ru/spisok-product/Tokio-FIT.html',
'https://www.tokyo-city.ru/spisok-product/deserty.html',
'https://www.tokyo-city.ru/spisok-product/childrensmenu.html',
'https://www.tokyo-city.ru/spisok-product/napitki1.html',
'https://www.tokyo-city.ru/new/',
'https://www.tokyo-city.ru/spisok-product/postnoe-menyu.html',
'https://www.tokyo-city.ru/hit/',
'https://www.tokyo-city.ru/vegetarian/',
'https://www.tokyo-city.ru/hot/',
'https://www.tokyo-city.ru/offers/',
'https://www.tokyo-city.ru/spisok-product/sauces.html',
'https://www.tokyo-city.ru/spisok-product/Pirogi-torty.html']
#
names_all = []
descriptions_all = []
prices_all = []
categories_all = []
url_all = []
weight_all = []
nutr_all = []
#
for url in urllist:
response = requests.get(url).text
soup = BeautifulSoup(response, features="html.parser")
items = soup.find_all('a', class_='item__name')
itemsURL = []
n = 0
for n, i in enumerate(items, start=n):
itemnotfullURL = i.get('href')
itemURL = 'https://www.tokyo-city.ru' + itemnotfullURL
itemsURL.extend({itemURL})
m = 0
namesList = []
descriptionsList = []
pricesList = []
weightList = []
nutrList = []
itemResponse = requests.get(itemURL).text
itemsSoup = BeautifulSoup(itemResponse, features="html.parser")
itemsInfo = itemsSoup.find_all('div', class_='item__full-info')
for m, u in enumerate(itemsInfo, start=m):
if (u.find('h1', class_='item__name') == None):
itemName = 'No data'
else:
itemName = u.find('h1', class_='item__name').text.strip()
if (u.find('p', class_='item__desc') == None):
itemDescription = 'No data'
else:
itemDescription = u.find('p', class_='item__desc').text.strip()
if (u.find('span', class_='item__price-value') == None):
itemPrice = '0'
else:
itemPrice = u.find('span', class_='item__price-value').text
if (u.find('div', class_='nutr-value') == None):
itemNutr = 'No data'
else:
itemNutr = u.find('div', class_='nutr-value').text.strip()
if (u.find('div', class_='item__weight') == None):
itemWeight = '0'
else:
itemWeight = u.find('div', class_='item__weight').text.strip()
namesList.extend({itemName})
descriptionsList.extend({itemDescription})
pricesList.extend({itemPrice})
weightList.extend({itemWeight})
nutrList.extend({itemNutr})
df = pd.DataFrame((
{'Name': namesList,
'Description': descriptionsList,
'Price': pricesList,
'Weight': weightList,
'NutrInfo': nutrList
}))
names_all.extend(df['Name'])
descriptions_all.extend(df['Description'])
prices_all.extend(df['Price'])
weight_all.extend(df['Weight'])
nutr_all.extend(df['NutrInfo'])
df['Category'] = soup.find('div', class_='title__container').text.strip()
categories_all.extend(df['Category'])
result = pd.DataFrame((
{'Name': names_all,
'Description': descriptions_all,
'Price': prices_all,
'Category': categories_all,
'NutrInfo': nutr_all,
'Weight': weight_all,
}))
print(" : " + str(datetime.datetime.now()))
- / . , . , , , - - .
- .
:
, , , , , , .
:
. , . , , , , .
City
“ City” 19 5 , , (, ). - 351.
“” - 13 , 301 . , “ City” , 40% , , , “”.
2
- 241 15 .
, , , .
№1: ?
, “” .
, , , . , “”, “ ”, “”, “”, “” “” + .
:
, “2 ” - №1 . , “” (“ City” - 20, “” - 17 “2 ” - 51).
- , “” .
№2: ?
, - “”, “” “ ”. . , , “ ”, .
100 , :
“2 ” , “ ”. , “ + ” . “ ” “ City” “”.
“ City” . “2 ” 2 . “” . 100 “” 30% ( , ), “ City” .
, :
“” . “” 30% .
“2 ” , . , “ ” , , 2 + ( ). .
“ City” .
№3: ?
, , . , .
“ City” 205 100 , . , . “2 ” 35% , . , , , .
: ?
, , , , .
100グラムあたりのカロリーが最も高く、ファーストフードが多いにもかかわらず、「2ショアーズ」はかなりバランスの取れたメニューを提供し、同じ「東京市」は炭水化物に対して明確な偏見を持っています。
BZHUの「ユーラシア」はどういうわけか均一すぎて、実質的に排出物がないので、疑惑が生じます。
一般に、私がこの質問で具体的に行った結論の正しさについては疑問があります。おそらく、質問に対する正解のために、これらの指標を何らかの方法で評価する必要があります。
これはとても小さいですが、好奇心が強いのですが、私の意見では、研究は「何かを解析する」というランダムな考えから生まれました。