【问题标题】:BeautifulSoup does not return resultsBeautifulSoup 不返回结果
【发布时间】:2017-07-17 11:04:43
【问题描述】:

我正在尝试制作一个 scraper,它将打印出此 url 上的所有房屋事件:

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())

【问题讨论】:

  • 如果应该查找类为 genre_list 的 div 元素,“无结果”是唯一正确的结果,因为您的输入 HTML 不包含单个这样的元素。跨度>
  • 当我检查页面时,我看到了它。你会怎么写呢?

标签: python python-3.x web-scraping beautifulsoup


【解决方案1】:

您不是在寻找正确的元素。您需要从寻找具有holdevents 类的div 开始。然后查找包含House 的dl 属性。如果找到,请刮掉标题和日期并添加到列表中。

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)

这会吸引你:

[('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')]

【讨论】:

  • 放入后仍然没有结果。
  • @Orophix 您没有正确复制代码。现在看。我已经在我的程序中发布了完整的代码。
  • 非常感谢 Coldspeed!
【解决方案2】:

您应该再次检查 HTML 代码。没有带有 class="genre_list"

的 div 标签

阅读本文以获取有关如何使用 bs4 进行抓取的更多信息: http://web.stanford.edu/~zlotnick/TextAsData/Web_Scraping_with_Beautiful_Soup.html

【讨论】:

    【解决方案3】:
    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())
    

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2015-05-18
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      相关资源
      最近更新 更多