2017-07-17 193 views
0

我試圖做一個刮板,將打印出該網址的所有房子的事件:BeautifulSoup沒有返回結果

https://iflyer.tv/en-jp/listing/events/on/2017-07-22/

但我回去與上面的代碼沒有結果,任何想法爲什麼?

from bs4 import BeautifulSoup 
import requests 

headers = {'User-Agent': 'Mozilla/5.0'} 
response = requests.get("http://iflyer.tv/en-jp/listing/events/on/2017-07-22/") 
soup = BeautifulSoup(response.text, "html.parser") 

results= soup.find_all('div', {"class": "genre_list"}) 

for result in results: 
    print(result.find('HOUSE').get_text()) 
+1

,「沒有結果」是唯一正確的結果,因爲您的輸入HTML不包含一個這樣的元素。 – CBroe

+0

當我檢查頁面時,我可以看到它。你會如何寫它? – Orophix

回答

1

你不是在尋找合適的元素。您需要先查找div,該類的類別爲holdevents。然後查找包含Housedl屬性。如果找到,則刮標題和日期並添加到列表中。

from bs4 import BeautifulSoup 
import requests 

headers = {'User-Agent': 'Mozilla/5.0'} 
response = requests.get("http://iflyer.tv/en-jp/listing/events/on/2017-07-22/") 
soup = BeautifulSoup(response.text, "html.parser") 
events = soup.find_all('div', {"class": "holdevent"}) 

house_events = [] 
for event in events: 
    genre_list = event.find('dl', {"class": "genre_list"}) 

    if genre_list.find(text='House'): 
     title = event.find('h1', {'class' : 'title'}).a.text 
     date = event.find('h1', {'class' : 'nicedate'}).text 
     house_events.append((title, date)) 

print(house_events) 

這會爲你抓取:如果是應該找genre_list``div`元素與類`

[('Tropical Disco fueled by Chandon Passion', 'SAT, 22 Jul 2017'), 
('West House Crossover Connection VOL.5 -Zakuro 1st Anniversary', 'SAT, 22 Jul 2017'), 
('SUBCULTURE', 'SAT, 22 Jul 2017')] 
+0

放入時仍然沒有結果。 – Orophix

+0

@Orophix您沒有正確複製代碼。現在看。我已經在程序中發佈了完整的代碼。 –

+1

非常感謝Coldspeed! – Orophix

0
from bs4 import BeautifulSoup 
import requests 

headers = {'User-Agent': 'Mozilla/5.0'} 
response = requests.get("http://iflyer.tv/en-jp/listing/events/on/2017-07-22/") 
soup = BeautifulSoup(response.text, "html.parser") 

results = soup.find_all('div', {"class": "holdevent"}) 
for result in results: 
    print('event') 
    print(result.find('h1', {"class": "nicedate"}).get_text()) 
    print(result.find('h1', {"class": "title"}).get_text())