编写基本的抓取脚本。我无法理解我哪里出错了。当我使用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()
答案 0 :(得分:1)
base.encode('utf-8)')
encoding_base.decode('utf-8)')
删除)
base.encode('utf-8')
encoding_base.decode('utf-8')
这是我看到的第一个,但我需要和错误
在代码中的某处,您需要将ingredrients
更改为ingredients
检查