【问题标题】:Printing accents and foreign characters using beautiful soup and python使用漂亮的汤和 python 打印口音和外来字符
【发布时间】:2016-11-05 22:47:04
【问题描述】:

我正在从 discogs.com 抓取艺术家。我无法获取页面上显示的艺术家姓名。例如。当我运行我的代码时,艺术家 Andrés 显示为 Andr\xe9s。

谁能解释我做错了什么?

    from bs4 import BeautifulSoup
    import requests
    import urllib2
    from itertools import chain
    import codecs

    headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0' }

    all_artists = []

    result_pages = 1 #446

    def load_artists():
        for page in xrange(1, result_pages+1):
            url = url = 'https://www.discogs.com/search/?sort=have%2Cdesc&style_exact=House&genre_exact=Electronic&decade=2010&page=' + str(page)
            r = requests.get(url, headers = headers)
            soup = BeautifulSoup(r.content.decode('utf-8'), 'html.parser')
            [all_artists.append(tag["title"]) for tag in soup.select('div#search_results h5 span')]

    load_artists()

    all_artists

【问题讨论】:

    标签: python html parsing unicode beautifulsoup


    【解决方案1】:

    你需要使用python3,你将不再受此困扰

    【讨论】:

      【解决方案2】:

      没有什么是错误,它们以 unicode 形式输出,当您要求 Python 打印它们时它们会正确打印:

      for a in all_artists:
          print(a)
      
      ...
      Andrés
      ...
      

      【讨论】:

      • 如何将它存储在我的列表中,以便它以与打印相同的方式存储?我需要在搜索查询中使用这些名称
      • 将其存储为 unicode 会更安全一些。这取决于您正在搜索的服务。有关示例,请参见 here。
      猜你喜欢
      • 2014-12-19
      • 2018-11-03
      • 2015-09-09
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多