【发布时间】:2023-03-03 12:30:01
【问题描述】:
我正在使用此代码:
import lxml.etree as et
import os
import glob
import contextlib
@contextlib.contextmanager
def stdout2file(fname):
import sys
f = open(fname, 'w')
sys.stdout = f
yield
sys.stdout = sys.__stdout__
f.close()
def skip_to(fle, line):
with open(fle) as f:
pos = 0
cur_line = f.readline().strip()
while not cur_line.startswith(line):
pos = f.tell()
cur_line = f.readline()
f.seek(pos)
return et.parse(f)
def trade_spider():
os.chdir(r"F:\ABC")
with stdout2file("Test123.txt"):
for file in glob.iglob('**\*.html', recursive=True):
xml = skip_to(file, "<?xml")
tree = xml.getroot()
nsmap = {"ix": tree.nsmap["ix"]}
fractions = xml.xpath("//ix:nonFraction[contains(@name, 'ABC')]", namespaces=nsmap)
for fraction in fractions:
print(file.split(os.path.sep)[-1], end="| ")
print(fraction.get("name"), end="| ")
print(fraction.text, end="|" "\n")
break
trade_spider()
我收到此错误消息:
C:\Users\Anaconda3\python.exe C:/Users/PycharmProjects/untitled/Versuch/lxmlparser.py
Traceback (most recent call last):
File "C:/PycharmProjects/untitled/Versuch/lxmlparser.py", line 42, in <module>
trade_spider()
File "C:/6930p/PycharmProjects/untitled/Versuch/lxmlparser.py", line 33, in trade_spider
xml = skip_to(file, "<?xml")
File "C:/6930p/PycharmProjects/untitled/Versuch/lxmlparser.py", line 26, in skip_to
return et.parse(f)
File "lxml.etree.pyx", line 3427, in lxml.etree.parse (src\lxml\lxml.etree.c:79720)
File "parser.pxi", line 1803, in lxml.etree._parseDocument (src\lxml\lxml.etree.c:116182)
File "parser.pxi", line 1823, in lxml.etree._parseFilelikeDocument (src\lxml\lxml.etree.c:116474)
File "parser.pxi", line 1718, in lxml.etree._parseDocFromFilelike (src\lxml\lxml.etree.c:115235)
File "parser.pxi", line 1139, in lxml.etree._BaseParser._parseDocFromFilelike (src\lxml\lxml.etree.c:110109)
File "parser.pxi", line 573, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:103323)
File "parser.pxi", line 679, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:104936)
File "lxml.etree.pyx", line 324, in lxml.etree._ExceptionContext._raise_if_stored (src\lxml\lxml.etree.c:10656)
File "parser.pxi", line 362, in lxml.etree._FileReaderContext.copyToBuffer (src\lxml\lxml.etree.c:100828)
File "C:\6930p\Anaconda3\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 1789: character maps to <undefined>
对于以下内容,我的目录由 5 个子文件夹组成,每个子文件夹最多包含 12 个填充 HTML 文件的子文件夹。
如果我将os.chdir(r"F:\ABC\201X\XXX") 设置为目录“201X”中的每个子文件夹,则代码可以完美运行。但是它会给我上面提到的错误消息如果:
1. 我设置了os.chdir as r"F:\ABC\2012\October(10 月是 2012 年子文件夹中 lxml 解析器正在解析的第一个子文件夹。(对于所有其他子文件夹,这非常有效!?)
2.如果我设置os.chdir as r"F:\ABC。因为我不想手动设置所有子文件夹,所以我最初的意图必须是自动解析 ABC 中的所有子文件夹。我想如果我使用for file in glob.iglob('**/*.html', recursive=True): 它会浏览包含在我的目录“ABC”中的所有子文件夹?
有人遇到过类似的问题吗?
【问题讨论】:
-
试试,
with open(fle,encoding="utf=8") as f -
再次非常感谢@PadraicCunningham!它在我的第一个子文件夹“October”以及用于解析所有包含子文件夹的子文件夹“2012”上完美运行。
-
不用担心,您可以将其添加为关闭问题的答案。
-
所以又是我....该代码非常适合我在“04_Independent....”中的第一个子文件夹“2012”以及它包含的所有子文件夹“十月、十一月、十二月”。但是,我在其他一些子文件夹上随机测试了代码,似乎它不适用于所有人。代码已执行,但之后不会停止运行。创建了一个 txt 输出文件,但它是空的....任何想法是什么导致了这个问题?除了 utf=8 编码,我没有对代码进行任何更改。我不明白,为什么它在某些子文件夹上工作,而在其他一些子文件夹上却不行?!
-
P.S.我使用我的 BS4 版本作为替代,它可以在所有子文件夹中正常工作,没有问题。但正如我在另一篇文章中提到的,BS4 在解析大量 HTML 文件时速度很慢。获得最终的 txt 输出文件需要很长时间......
标签: python-3.x directory pycharm lxml