bytes对象没有属性find_all

时间:2016-07-08 07:10:40

标签: web-scraping byte python-requests bs4

我在最近3个小时内一直试图抓住这个website并获得每个团队的排名,名称,胜利和损失。

实施此代码时:

import requests
from bs4 import BeautifulSoup

halo = requests.get("https://www.halowaypoint.com/en-us/esports/standings")

page = BeautifulSoup(halo.content, "html.parser")

final = page.encode('utf-8')

print(final.find_all("div"))

我一直得到这个error

如果有人可以帮助我,那将非常感激!

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在调用错误变量上的方法,使用BeautifulSoup对象 而不是字节字符串 final

print(page.find_all("div"))

要获取表数据非常简单,所有数据都在div中,css类为“table.table - hcs”

halo = requests.get("https://www.halowaypoint.com/en-us/esports/standings")

page = BeautifulSoup(halo.content, "html.parser")


table = page.select_one("div.table.table--hcs")
print(",".join([td.text for td in table.select("header div.td")]))
for row in table.select("div.tr"):
    rank,team = row.select_one("span.numeric--medium.hcs-trend-neutral").text,row.select_one("div.td.hcs-title").span.a.text
    wins, losses = [div.span.text for div in row.select("div.td.em-7")]
    print(rank,team, wins, losses)

如果我们运行代码,您可以看到数据与表格匹配:

In [4]: print(",".join([td.text for td in table.select("header div.td")]))
Rank,Team,Wins,Losses

In [5]: for row in table.select("div.tr"):
   ...:         rank,team = row.select_one("span.numeric--medium.hcs-trend-neutral").text,row.select_one("div.td.hcs-title").span.a.text
   ...:         wins, losses = [div.span.text for div in row.select("div.td.em-7")]
   ...:         print(rank,team, wins, losses)
   ...:     
1  Counter Logic Gaming 10 1
2  Team EnVyUs 8 3
3  Enigma6 8 3
4  Renegades 6 5
5  Team Allegiance 5 6
6  Evil Geniuses 4 7
7  OpTic Gaming 2 9
8  Team Liquid 1 10