【发布时间】: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