【问题标题】:A basic UnicodeEncodeError issue一个基本的 UnicodeEncodeError 问题
【发布时间】:2018-05-17 18:34:45
【问题描述】:

编写基本的抓取脚本。我无法理解我一直出错的地方。我在使用 python 2.7 时遇到了同样的问题。

我使用 Sublime 3 作为我的文本编辑器 我使用 macOS 我运行python3.6

这是我目前所拥有的:

import bs4

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'http://wbsite.com'

uClient = uReq(my_url)

page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")

#loop starts here

#base of the script,

one = page_soup.findAll("div",{"class":"large-7 medium-9 columns"})
two = page_soup.findAll("div",{"body field"})
three = page_soup.findAll("div",{"field-ingredients-data field-wrapper"})
four = page_soup.findAll("div",{"field-how-to-use-data field-wrapper"})



onito = (one[0].h1)
docito = two[1]
trincipito = (three[0].p)
cuatrocinto= (four[0].p)


filename = "insertfilehere.cvs"
f = open(filename, "w")
headers = "onito, docito, trincipito, cuatrocinto\n"

f.write(headers)

un = str(onito.text)
deux = str(docito.text)
trois = str(trincipito.text)
quatre = str(cuatrocinto.text)


base = (un + "," + deux + "," + trois + "," + quatre + "\n")
encoding_base = base.encode("utf-8)")
finito = encoding_base.decode("utf-8)")

f.write(finito)

f.close()

请帮助我理解为什么我如此愚蠢以至于无法解决这个小问题。

错误:

Traceback (most recent call last):
File "/Users/user/Downloads/scrape.py", line 74, in<module>
f.write(un + "," + deux + "," + trois + "," + quatre + "\n")
UnicodeEncodeError: 'ascii' codec can't encode character '\u2022' in    position 30: ordinal not in range(128)
[Finished in 0.8s]

*眼泪

这是我更新的脚本。谢谢大家的帮助!!!

            import bs4

            from urllib.request import urlopen as uReq
            from bs4 import BeautifulSoup as soup
            my_url = 'https://www.insertsitehere.com/'

            uClient = uReq(my_url)

            page_html = uClient.read()
            uClient.close()

            page_soup = soup(page_html, "html.parser")

            one = page_soup.findAll("div",{"class":"large-7 medium-9 columns"})
            two = page_soup.findAll("div",{"body field"})
            three = page_soup.findAll("div",{"field-ingredients-data field-wrapper"})
            four = page_soup.findAll("div",{"field-how-to-use-data field-wrapper"})



            uno = (one[0].h1)
            dos = two[1]
            tres = (three[0].p)
            cuatro = (four[0].p)


            filename = "example.csv"
            f = open(filename, "w",encoding="utf-8")

            headers = "one, two, three, four\n"
            f.write(headers)


            un = str(uno_en.text)
            deux = str(dos.text)
            trois = str(tres.text)
            quatre = str(cuatro.text)

            f.write(un + "," + deux + "," + trois + "," + quatre + "\n")

            f.close()

【问题讨论】:

  • 你能包含错误信息吗?
  • 您遇到的错误是什么?
  • 请修改您的问题以包含错误格式。尝试使用降价编辑,使其格式正确且易于阅读。
  • 嗯,这似乎与 Unicode 错误没有任何关系。
  • Traceback(最近一次调用最后):文件“/Users/user/Downloads/scrape.py”,第 74 行,in f.write(un + "," + deux + " ," + trois + "," + quatre + "\n") UnicodeEncodeError: 'ascii' codec can't encode character '\u2022' in position 30: ordinal not in range(128) [Finished in 0.8s]跨度>

标签: python python-3.x web-scraping


【解决方案1】:
base.encode('utf-8)')
encoding_base.decode('utf-8)')

删除)

base.encode('utf-8')
encoding_base.decode('utf-8')

这是我第一次看到,但我需要和错误

您需要在代码中的某处将 ingredrients 更改为 ingredients 进行检查

【讨论】:

  • Traceback(最近一次调用最后):文件“/Users/user/Downloads/scrape.py”,第 74 行,in f.write(un + "," + deux + " ," + trois + "," + quatre + "\n") UnicodeEncodeError: 'ascii' codec can't encode character '\u2022' in position 30: ordinal not in range(128) [Finished in 0.8s]跨度>
  • @ninodelaluna 为什么你编码然后解码?你可以注释掉解码并尝试运行脚本,这个错误也会显示吗?试试f.write(encoding_base)
  • 目前,我需要抓取四个项目。 un, deux, trois & quatre。 Deux 是我假设存在无法解码的字符的地方。没有那个脚本可以正常运行,这真的很奇怪。
  • 脚本已更新,现在运行良好。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2018-03-14
  • 2011-06-16
  • 2012-09-24
  • 2011-11-26
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
相关资源
最近更新 更多