gpt4 book ai didi

python - 使用 python 解析 PDF 在 cssselect 上失败

转载 作者:行者123 更新时间:2023-12-01 04:27:21 24 4
gpt4 key购买 nike

尝试使用 pdfquery、PDFDocument 和 PDFParser 解析 PDF 文件。还尝试了 pdfquery 示例代码。总是从 cssselect/parser.py 中收到错误

SelectorSyntaxError: Expected string or ident

尝试升级 cssselect,但没有成功。

有人知道吗?我正在运行 Python 2.7

我使用的代码和示例文件来自:https://github.com/jcushman/pdfquery

pdf = pdfquery.PDFQuery("tests/samples/IRS_1040A.pdf")
pdf.extract( [
('with_parent','LTPage[pageid=1]'),
('with_formatter', 'text'),
('last_name', 'LTTextLineHorizontal:in_bbox("315,680,395,700")'),
('spouse', 'LTTextLineHorizontal:in_bbox("170,650,220,680")'),
('with_parent','LTPage[pageid=2]'),
('oath', 'LTTextLineHorizontal:contains("perjury")', lambda match: match.text()[:30]+"..."),
('year', 'LTTextLineHorizontal:contains("Form 1040A (")', lambda match: int(match.text()[-5:-1]))
])

最佳答案

我查看了示例,似乎您缺少一些引号(在 xpath 表达式 LTPage[pageid="1"]LTPage[pageid="2"] 中) 。使用引号就不会出现错误消息。

pdf.extract( [
('with_parent','LTPage[pageid="1"]'),
('with_formatter', 'text'),
('last_name', 'LTTextLineHorizontal:in_bbox("315,680,395,700")'),
('spouse', 'LTTextLineHorizontal:in_bbox("170,650,220,680")'),
('with_parent','LTPage[pageid="2"]'),
('oath', 'LTTextLineHorizontal:contains("perjury")', lambda match: match.text()[:30]+"..."),
('year', 'LTTextLineHorizontal:contains("Form 1040A (")', lambda match: int(match.text()[-5:-1]))
])

关于python - 使用 python 解析 PDF 在 cssselect 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32902132/

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