【问题标题】:Handling Indian Languages in BeautifulSoup在 BeautifulSoup 中处理印度语言
【发布时间】:2013-01-19 09:24:33
【问题描述】:

我正在尝试在 NDTV 网站上搜索新闻标题。 This 是我用作 HTML 源的页面。我正在使用 BeautifulSoup (bs4) 来处理 HTML 代码,并且一切正常,除了当我在链接到的页面中遇到印地语标题时我的代码中断。

到目前为止我的代码是:

import urllib2
from bs4 import BeautifulSoup

htmlUrl = "http://archives.ndtv.com/articles/2012-01.html"
FileName = "NDTV_2012_01.txt"

fptr = open(FileName, "w")
fptr.seek(0)

page = urllib2.urlopen(htmlUrl)
soup = BeautifulSoup(page, from_encoding="UTF-8")

li = soup.findAll( 'li')
for link_tag in li:
   hypref = link_tag.find('a').contents[0]
   strhyp = str(hypref)
   fptr.write(strhyp)
   fptr.write("\n")

我得到的错误是:

Traceback (most recent call last):
  File "./ScrapeTemplate.py", line 30, in <module>
  strhyp = str(hypref)
  UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

即使我没有包含from_encoding 参数,我也会遇到同样的错误。我最初将它用作fromEncoding,但python警告我它已被弃用。

我该如何解决这个问题?从我读过的内容来看,我需要避免使用印地语标题或将其明确编码为非 ascii 文本,但我不知道该怎么做。任何帮助将不胜感激!

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    你看到的是一个 NavigableString 实例(它是从 Python unicode 类型派生的):

    (Pdb) hypref.encode('utf-8')
    'NDTV'
    (Pdb) hypref.__class__
    <class 'bs4.element.NavigableString'>
    (Pdb) hypref.__class__.__bases__
    (<type 'unicode'>, <class 'bs4.element.PageElement'>)
    

    您需要使用转换为 utf-8

    hypref.encode('utf-8')
    

    【讨论】:

      【解决方案2】:
      strhyp = hypref.encode('utf-8')
      

      http://joelonsoftware.com/articles/Unicode.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-17
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        • 2014-04-25
        • 1970-01-01
        相关资源
        最近更新 更多