将工作代码移动到单独的模块时出现 Unicode UTF8 错误



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

该代码是关于使用硒和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:UsersVoxPycharmProjectsinteriafunkcje.py", line 34
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xbf in position 58: invalid start byte

感谢马拉!

尝试将 # --编码:UTF-8 --放在新文件的顶部(替换 UTF-8 与任何编码用于 pokaż

已解决的问题...不知道为什么它首先会发生。像不是主文件的新文件默认不是utf-8?

最新更新