Python reads PDF content

Source: Internet
Author: User
1, Introduction

At night read the "Python Network data Collection" This book, see the code reading the PDF content, think of the previous few days the collection has just released a crawl page PDF content Crawl rules, this rule can be the PDF content as HTML to do Web crawling. The magic is due to the ability of Firefox to parse PDFs, to convert the PDF format into HTML tags, such as a div tag, to use Gooseeker web crawler software like a common Web page to grab structured content.

This creates a problem: how much can you do with a python crawler? An experimental procedure and source code are described below.

2. Python source code to convert PDF to text

The following Python source code reads the contents of the PDF file (on the Internet or locally), converts it into text, and prints it out. This code uses a third-party library pdfminer3k to read the PDF into a string and then convert it to a file object using Stringio. (Source code download address see GitHub Source at the end of the article)

From urllib.request import urlopenfrom pdfminer.pdfinterp import Pdfresourcemanager, Process_pdffrom Pdfminer.converter Import textconverterfrom pdfminer.layout import laparamsfrom io import stringiofrom io import opendef r Eadpdf (pdffile):    rsrcmgr = Pdfresourcemanager ()    retstr = Stringio ()    laparams = laparams ()    device = Textconverter (Rsrcmgr, Retstr, Laparams=laparams)    process_pdf (rsrcmgr, Device, Pdffile)    device.close ()    content = Retstr.getvalue ()    retstr.close ()    return contentpdffile = Urlopen ("http://pythonscraping.com/ Pages/warandpeace/chapter1.pdf ") outputstring = Readpdf (pdffile) print (outputstring) pdffile.close ()

If the PDF file is on your computer, replace the Urlopen returned object pdffile with the normal open () file object.

3, Outlook

The experiment just converted the PDF into text, but there was no conversion from the beginning to the HTML tag, so the ability to do so in a python programming environment was left to be explored in the future.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.