findAll为html返回空 [英] findAll returning empty for html

查看:307
本文介绍了findAll为html返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用BeautifulSoup模块来解析要从中提取某些信息的html文件.具体来说就是比赛得分和球队名称.

I'm using the BeautifulSoup module to parse an html file that I want to extract certain information from. Specifically game scores and team names.

但是,当我使用findAll函数时,对于肯定在html内的字符串,它会连续返回空.如果有人可以解释我在做什么错,将不胜感激.参见下面的代码.

However, when I use the findAll function, it continually returns empty for a string that is certainly within the html. If someone can explain what I am doing wrong it will be greatly appreciated. See code below.

import urllib
import bs4
import re
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'http://www.foxsports.com/mlb/scores?season=2017&date=2017-05-09'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
# html parser
page_soup = soup(page_html, "html.parser")
container = page_soup.findAll("div",{"class":"wisbb_teams"})
print(len(container))

推荐答案

我认为您使用的语法是BeautifulSoup的旧版本,请改用 find_all snake_case之类的东西(请参阅文档)

I think the syntax your using is the old version of BeautifulSoup, try instead something like find_all snake_case (see the docs)

from bs4 import BeautifulSoup
# ...
page_html = uClient.read()
page_soup = BeautifulSoup(page_html, "html.parser")
list_of_divs = page_soup.find_all("div", class_="wisbb_name")
print(len(list_of_divs))

较早的API使用的是CamelCase,而bs4使用的是snake_case

The older API used CamelCase, but bs4 uses snake_case

此外,find_all接受的通知可以采用class_参数来按类别查找.

Also, notice that find_all takes can take a class_ parameter to find by class.

请参阅以下答案, https://stackoverflow.com/a/38471317/4443226 ,以获取更多信息

See this answer, https://stackoverflow.com/a/38471317/4443226, for some more info

此外,请确保您正在寻找正确的类名!我没有看到您要寻找的课程,而是这些:

Also, make sure you're looking for the correct classname! I don't see the class you're looking for, but rather these:

这篇关于findAll为html返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆