2017-12-25 18 views
1
import requests 
from bs4 import BeautifulSoup 

res = requests.get('http://aicd.companydirectors.com.au/events/events-calendar') 
soup = BeautifulSoup(res.text,"lxml") 


event_containers = soup.find_all('div', class_ = "col-xs-12 col-sm-6 col-md-8") 

first_event = event_containers[0] 
print(first_event.h3.text) 

このコードを使用して、私はイベント名を抽出することができます。ループを作成し、すべてのイベント名と日付を抽出しようとしていますか?また、私はreadmoreリンクをクリックした後に見える位置情報を抽出しようとしていますPython:BeautifulSoup divクラスのすべての見出しテキストを抽出

+0

:' '' – Martin

+0

ちょうどのために、これを試してます私はちょうど映画をイベントに置き換えましたので、理解しやすくなりました。 – Mahesh

答えて

1

event_containersはオブジェクトです。これは基本的にTagオブジェクトのリストです。
だけのevent_containersのタグをループやURL、例えば日付のタイトルのためh3div.dateaを選択:

for tag in event_containers: 
    print(tag.h3.text) 
    print(tag.select_one('div.date').text) 
    print(tag.a['href']) 

今は、位置情報のためには、各URLを訪問する必要がありますし、 div.dateのテキストを収集します。
全コード:

import requests 
from bs4 import BeautifulSoup 

res = requests.get('http://aicd.companydirectors.com.au/events/events-calendar') 
soup = BeautifulSoup(res.text,"lxml") 
event_containers = soup.find_all('div', class_ = "col-xs-12 col-sm-6 col-md-8") 
base_url = 'http://aicd.companydirectors.com.au' 

for tag in event_containers: 
    link = base_url + tag.a['href'] 
    soup = BeautifulSoup(requests.get(link).text,"lxml") 
    location = ', '.join(list(soup.select_one('div.event-add').stripped_strings)[1:-1]) 
    print('Title:', tag.h3.text) 
    print('Date:', tag.select_one('div.date').text) 
    print('Link:', link) 
    print('Location:', location) 
+0

これは素晴らしいです。また、私は位置情報の検索を手伝ってくれますか? – Mahesh

+0

私は自分の答えを説明しようとしましたが、特定の質問がある場合は質問してください。位置情報については、リンクやテキストが必要ですか? –

+0

ロケーションテキストとイベントのハイパーリンクを探しています – Mahesh

1

すべてのイベントを取得するには、これを試してみて、あなたが後にある日付: `` movie_containersで映画の `

import requests 
from bs4 import BeautifulSoup 

res = requests.get('http://aicd.companydirectors.com.au/events/events-calendar') 
soup = BeautifulSoup(res.text,"lxml") 
for item in soup.find_all(class_='lead'): 
    date = item.find_previous_sibling().text.split(" |")[0] 
    print(item.text,date) 
+0

@ Shahinこれは素晴らしいです、また、私は位置情報を見つけるのを助けてくれますか? – Mahesh

+0

関連する情報を各 'date'に付けるには、この部分を' .split( "|")[0] 'から取り出してください。 – SIM

+0

ロケーション情報を取得するには、 'read more'オプションに接続されたリンクを解析し、別のhttpリクエストを作成する必要があります。しかし、それは別の投稿用です。メソッドの問題を理解する上で、私のpython guru sir t.m.adamがあなたに1つを提供しました。ありがとう。 – SIM

関連する問題