やりたいこと
Udemyの検索画面から、各ページのリンクや講座名、レートなどをスクレイピングするコードを作成したい。
問題
-
urlがうまく生成できていない
検索ページのurlの形式は分かっているが作成したコードでは文字コードエラーが出てしまいencodeしても動作しなかった。 -
そもそもどこをスクレイピングすれば良いかわからない
検索→一覧ページの取得は分かった。しかし、デベロッパーツールを確認したところターゲットが見つからず。恐らく、ページ遷移先でスクレイピングすると考えられるが、その場合の手段が調べても分からなかった。
以上二点です。
現状作成したurlを確認するところまででですが、下記コード参照していただければと思います。
python
1 2from csv import excel 3from tkinter.tix import DirSelectBox 4import urllib.request 5from bs4 import BeautifulSoup 6import lxml 7import selenium 8 9 10# 検索語群※ここではビジネス系の語群にしてみる 11key = ['人事'.encode('utf-8'),'HR','経理'.encode('utf-8'),'accounting','human+resources','営業'.encode('utf-8'),'EC',\ 12'marketing','マーケティング'.encode('utf-8'),'E+Commerce'] 13# 別の語群※ 14key2 = ['人事'.encode('utf-8'),'HR','経理'.encode('utf-8'),'accounting','human+resources','営業'.encode('utf-8'),'EC',\ 15'marketing','マーケティング'.encode('utf-8'),'E+Commerce'] 16#検索ページのうrl(+検索値) 17url = 'https://www.udemy.com/courses/search/?src=ukw&q=' 18 19""" 20ユーザーエージェント (User agent) 21確認くんでチェックする!! 22 23 https://www.ugtop.com/spill.shtml 24 25サイトによって、弾かれるから、ブラウザを偽装する必要があるらしい。 26""" 27 28#そのままコピペした 29ua = #そのままコピペした 30req = urllib.request.Request(url, headers={'User-Agent': ua}) 31html = urllib.request.urlopen(req) 32 33""" 34Python標準ライブラリのhtml.parser以外に、lxmlやhtml5libを指定することもできる。 35その場合は、それぞれをpipなどでインストールしておく必要がある。 36"html.parser"を使わないと警告出されるらしい 37 38$ pip install lxml 39$ pip install html5lib 40 41Beautiful Soupのドキュメントによるとlxmlが速いのでオススメとのこと。 42""" 43soup = BeautifulSoup(html, "lxml") 44 45""" 46恐らくここにページのリンクがある?? 47<div class="popper--popper-content--2tG0H course-objectives-quick-view-box--popover-wrapper--3rGCp" 48id="u206-popper-content--341" aria-labelledby="u206-popper-trigger--340"> 49 50<div class="popper--animation-wrapper--1uUMV"><div class="popover--popover--bps8g popover--popover--top--arrow--1CAPy"> 51<div class="popover--inner--1ZzRT"></div> 52<div class="popover--arrow--1i7d4 popover--arrow--top--17Jzn" style="top: 100%; left: 0rem;"></div></div></div></div> 53""" 54 55for k in range(len(key)): 56 Url = url+key[k] 57 for d in range(len(DSkey)): 58 Url = Url+key2[d] 59 print(Url) 60 61 62 63""" 64参考にしたコードの残差 65 66topicsindex = soup.find('div', attrs={'class': 'topicsindex'}) 67topics = topicsindex.find_all('li') 68 69for topic in topics: 70 print(topic.find('a').contents[0]) 71 72 73""" 74

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。