我正在尝试从澳大利亚证券交易所网站创建一个pdf取件器,这将允许我搜索公司制作的所有"公告",并在这些公告的pdf中搜索关键词.
到目前为止,我正在使用请求和PyPDF2获取PDF文件,将其写入我的驱动器然后读取它.但是,我希望能够跳过将PDF文件写入驱动器并读取它的步骤,并直接从获取PDF文件转换为字符串.到目前为止我所拥有的是:
import requests, PyPDF2
url = 'http://www.asx.com.au/asxpdf/20171108/pdf/43p1l61zf2yct8.pdf'
response = requests.get(url)
my_raw_data = response.content
with open("my_pdf.pdf", 'wb') as my_data:
my_data.write(my_raw_data)
open_pdf_file = open("my_pdf.pdf", 'rb')
read_pdf = PyPDF2.PdfFileReader(open_pdf_file)
num_pages = read_pdf.getNumPages()
ann_text = []
for page_num in range(num_pages):
if read_pdf.isEncrypted:
read_pdf.decrypt("")
print(read_pdf.getPage(page_num).extractText())
page_text = read_pdf.getPage(page_num).extractText().split()
ann_text.append(page_text)
else:
print(read_pdf.getPage(page_num).extractText())
print(ann_text)
Run Code Online (Sandbox Code Playgroud)
这将从提供的URL打印PDF文件中的字符串列表.
只是想知道我是否可以将my_raw_data变量转换为可读字符串?
非常感谢提前!
我正在尝试从澳大利亚证券交易所网站创建一个 pdf puller,这将允许我搜索公司发布的所有“公告”,并在这些公告的 pdf 文件中搜索关键词。
到目前为止我所做的是使用请求库。以下是我到目前为止的代码:
import requests
url = 'http://www.asx.com.au/asxpdf/20171103/pdf/43nyyw9r820c6r.pdf'
response = requests.get(url)
print(response.content)
Run Code Online (Sandbox Code Playgroud)
然而,打印的是以下字符串(我将把它剪掉,因为它太长了):
> b'%PDF-1.5\r%\xe2\xe3\xcf\xd3\r\n5 0 obj\r<</E 212221/H [ 1081 145 ]/L
> 212973/Linearized 1/N 1/O 8/T 212553>>\rendobj\r
> \r\r42 0 obj\r<</DecodeParms <</Columns 5/Predictor 12>>/Encrypt 7 0
> R/Filter /FlateDecode/ID [(\\216\\203\\217T\\n\\f\\236\\345?%\\214t4
> E\\271) (\\216\\203\\217T\\n\\f\\236\\345?%\\214t4 E\\271)]/Index [5
> 38]/Info 3 0 R/Length 86/Prev 212554/Root 6 0 R/Size 43/Type /XRef/W
> [1 3
> 1]>>\rstream\nx\x9ccbd`\x10``b``:\x04"\x19\xab\xc1d-X\xc4\x06D2\xac\x02\xb3\x93\xc0\xe2\x1d
> \x92?\x07,\x1e\t"\xb9T\x80$\xe3\x84\xcb@\x92\xa9m"\x03\x13\xe3\xdf\x13Z`Y\x06\xc6\x01#\xff3\xb0h\xbcfb`\xb6\x12\x02\xba\xe4\xef!S\x06\x0
Run Code Online (Sandbox Code Playgroud)
我已经搜索了 stackexchange 和其他网站几天,并尝试使用print(response.content.decode('utf-8')ascii,但它们都不是我能阅读的任何内容。
很抱歉,我知道很明显我是个菜鸟,但任何帮助将不胜感激!
非常感谢。