【问题标题】:Parsing html with correct encoding使用正确的编码解析 html
【发布时间】:2020-05-20 06:19:01
【问题描述】:

我正在尝试将basketball-reference API 与requestsbs4 库一起使用。

from requests import get
from bs4 import BeautifulSoup

这是我正在尝试做的一个最小示例:

# example request
r = get(f'https://widgets.sports-reference.com/wg.fcgi?css=1&site=bbr&url=%2Fteams%2FMIL%2F2015.html&div=div_roster')
soup = BeautifulSoup(dd.content, 'html.parser')
table = soup.find('table')

一切正常,然后我可以将这个table 和它的read_html 提供给pandas,然后将我需要的数据很好地打包到一个数据框中。

我遇到的问题是编码。

在这个特殊的请求中,我得到了两个带有奇怪字符的 NBA 球员姓名:Ersan İlyasova (Ersan İlyasova) 和 Jorge Gutiérrez (Jorge Gutiérrez)。在当前代码中,它们被解释为“Ersan İlyasova”和“Jorge Gutiérrez”,这显然不是我想要的。

所以问题是——我该如何解决它? This website 似乎暗示他们有 windows-1251 编码,但我不确定如何使用该信息(事实上我什至不确定这是否属实)。

我知道我在这里遗漏了一些基本的东西,因为我有点困惑这些编码是如何工作的,在什么时候它们被“解释”等等,所以如果你能帮我解决这个问题,我将不胜感激!

【问题讨论】:

    标签: python encoding beautifulsoup utf-8 python-requests


    【解决方案1】:

    我真的不知道你为什么使用format string,甚至你的问题也不清楚。您只需从网络流量中复制/粘贴 url,然后将有关带引号的字符串与编码混合在一起。

    下面你应该可以做到的。

    import pandas as pd
    
    
    df = pd.read_html("https://www.basketball-reference.com/teams/MIL/2015.html")
    
    print(df)
    

    输出:

    [    No.                   Player Pos  ... Unnamed: 6  Exp                College   
    0    34    Giannis Antetokounmpo  SG  ...         gr    1                    NaN    
    1    19           Jerryd Bayless  PG  ...         us    6                Arizona    
    2     5  Michael Carter-Williams  PG  ...         us    1               Syracuse    
    3     9             Jared Dudley  SG  ...         us    7         Boston College    
    4    11              Tyler Ennis  PG  ...         ca    R               Syracuse    
    5    13          Jorge Gutiérrez  PG  ...         mx    1             California    
    6    31              John Henson   C  ...         us    2                    UNC    
    7     7           Ersan İlyasova  PF  ...         tr    6                    NaN    
    8    23            Chris Johnson  SF  ...         us    2                 Dayton    
    9    11           Brandon Knight  PG  ...         us    3               Kentucky    
    10    5         Kendall Marshall  PG  ...         us    2                    UNC    
    11    6            Kenyon Martin  PF  ...         us   14             Cincinnati    
    12    0                O.J. Mayo  SG  ...         us    6                    USC    
    13   22          Khris Middleton  SF  ...         us    2              Texas A&M    
    14    3          Johnny O'Bryant  PF  ...         us    R                    LSU    
    15   27            Zaza Pachulia   C  ...         ge   11                    NaN    
    16   12            Jabari Parker  PF  ...         us    R                   Duke    
    17   21            Miles Plumlee   C  ...         us    2                   Duke    
    18    8            Larry Sanders   C  ...         us    4  Virginia Commonwealth    
    19    6             Nate Wolters  PG  ...         us    1     South Dakota State 
    

    【讨论】:

    猜你喜欢
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2014-01-20
    • 1970-01-01
    • 2013-02-24
    • 2013-12-10
    • 2016-01-21
    相关资源
    最近更新 更多