gpt4 book ai didi

python - 将工作代码移动到单独的模块时出现 unicode utf8 错误

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:36 25 4
gpt4 key购买 nike

我是一个Python新手程序员。我正在努力解决奇怪的错误 - 仅当我将工作代码作为函数从主脚本文件移动到单独的模块(文件)时才会弹出。错误是 SyntaxError: (unicode error) 'utf-8' 编解码器无法解码位置 58 中的字节 0xbf:无效的起始字节。如果该函数位于主代码中,则没有错误并且代码可以正常工作...

该代码是关于使用 selenium 和 xpath 进行一些网页抓取

#main file:
import requests
import lxml.html as lh
import pandas as pd
import numpy
import csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup

import funkcje as f
spolka = "https://mojeinwestycje.interia.pl/gie/prof/spolki/notowania?wlid=213"
wynik = f.listaTransakcji(spolka)

#module file with function definition (funkcje.py):
def listaTransakcji(spolka):
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get(spolka)
driver.find_element_by_xpath("//button[@class='rodo-popup-agree']").click()
driver.find_element_by_xpath("//input[@type='radio' and @name='typ' and @value='wsz']").click()
driver.find_element_by_xpath("//input[@type='submit' and @name='Submit' and @value='pokaż']").click()
page = driver.page_source
#end of selenium-----------------------------------------------------------------------------
#Store the contents of the website under doc
doc = lh.fromstring(page)
#wyluskanie rekordów transakcji - xpath------------------------------------------------------
tr_elements = doc.xpath('//table//tr[@bgcolor="#FFFFFF" or @bgcolor="#F7FAFF"]/td')
rekord = numpy.array([])
length = len(tr_elements)
for i in range (0, length):
if(tr_elements[i].text=='TRANSAKCJA') or (tr_elements[i].text=='WIDEŁKI STAT') or (tr_elements[i].text=='WIDEŁKI DYN'):
new_rekord=[tr_elements[i-5].text, tr_elements[i-4].text, tr_elements[i-3].text, tr_elements[i-2].text, tr_elements[i-1].text, tr_elements[i].text]
rekord=numpy.concatenate((rekord,new_rekord))

ilosc = (len(rekord))//6
tablica = numpy.array([])
tablica = rekord.reshape(ilosc, 6)
header = numpy.array(["godzina", "cena", "zmiana", "wolumen", "numer", "typ operacji"])
header = header.reshape(1, 6)
tablica = numpy.concatenate((header,tablica))
return (tablica)

有问题的第 10 行:

import funkcje as f

有问题的第 34 行:

driver.find_element_by_xpath("//input[@type='submit' and @name='Submit' and @value='pokaż']").click()

预期结果:

["11:17:40","0,4930","0,00",24300,76,"TRANSAKCJA"]

实际结果:

Traceback (most recent call last):
File "C:/Users/Vox/PycharmProjects/interia/scraper.py", line 10, in <module>
import funkcje as f
File "C:\Users\Vox\PycharmProjects\interia\funkcje.py", line 34
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xbf in position 58: invalid start byte

最佳答案

谢谢马拉!

try putting # -- coding: utf-8 -- on top of the new file (replace utf-8 with whatever encoding is used for pokaż

问题已解决...但不知道为什么会发生这种情况。就像不是主文件的新文件默认不是 utf-8 一样?

关于python - 将工作代码移动到单独的模块时出现 unicode utf8 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57125305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com