Сканирование в Интернете / сканирование определенной книги Google

2342
user2661243

Для моей работы мне нужно почистить текст из большой книги в Google Книгах. Эта книга очень старая и не защищена авторским правом. Книга представляет собой газету мира. Мы будем помещать текст в базу данных, поэтому нам нужен необработанный текст, а не PDF.

Я уже потратил много времени на изучение инструментов и методов, которые можно использовать для выполнения этой задачи. Я чувствую себя подавленным и не знаю, с чего начать или какой метод лучше всего использовать. Я не хочу тратить больше времени на тупик.

Проблема может быть разделена на две части:

  1. Ползать по страницам.
  2. Загрузка данных.

Это действительно часть (1), на которой я больше всего застрял. Когда у меня есть данные (даже если это только необработанные HTML-страницы), я уверен, что смогу использовать парсер для извлечения того, что я хочу.

Навигация по страницам осуществляется нажатием кнопки «Продолжить» или стрелки. Приращение страницы не всегда соответствует, оно может варьироваться, потому что на некоторых страницах есть встроенные изображения. Поэтому я не могу предсказать следующий URL. Первоначальный URL для тома 1 книги:

http://books.google.co.uk/books?id=grENAAAAQAAJ&pg=PR5&output=text

Я могу программировать на Java и JavaScript, и у меня есть базовые знания Python. Я рассмотрел node.js и scrapy среди многих других вещей. Я попробовал wget, но получил 401 ошибку неавторизованного доступа. Также я попробовал iRobot, GreaseMonkey и FoxySpider.

2

2 ответа на вопрос

1
Jan Doggen

If I do a Google search on download "Gazetteer of the World" dictionary of geographic knowledge, I see it available in e.g. PDF, and then something like PDF2Word kan be used to extract the text;

Unless the PDF is all pics ;-) Then you could try extracting with pdf2jpg and feed the image files intro an OCR program.

You can also buy them (Amazon has several hits), cut the pages out and pull these through a scanner with autofeed and OCR.

Since this is a one-time effort, programming would be my very last resort.

It might help if you first make an inventory of the format in which you are able to acquire the (6?) volumes, and an estimate of the costs and amount of work processing these formats will take.

Спасибо за предложения. Я пробовал PDF2Word, но это не сработало, я думаю, потому что текст в файле - это изображение. Я подумал об оптическом распознавании PDF-файла, но это не очень высокое разрешение, и я думаю, что будут проблемы, связанные с этим. user2661243 10 лет назад 0
1
user2661243

I solved this problem by writing a small program (called extract.js) in node.js to scrape the text. I used this page to help me: http://blog.miguelgrinberg.com/post/easy-web-scraping-with-nodejs

Each html page contains multiple book pages. Therefore if we increment the page parameter in the url only by 1 then we would be scraping duplicate book pages if we are not careful (this was the part I was particularly stuck on). I got around this by using a jquery selector to only select the individual book page specified in the url and to ignore the other book pages present in the html. This way I could quickly construct a text file using a spreadsheet program with the urls for each single page in order (because the increment is only 1).

So far I have successfully scraped the first two volumes, five more to go! The code is given below, it may serve as a useful starter for scraping other Google books.

// Usage: node extract.js input output // where input (mandatory) is the text file containing your list of urls // and output (optional) is the directory where the output files will be saved var fs = require('fs'); var request = require('request'); var cheerio = require('cheerio'); // Read the command line parameters var input = process.argv[2]; var output = process.argv[3]; if (!input) { console.log("Missing input parameter"); return; } // Read the url input file, each url is on a new line var urls = fs.readFileSync(input).toString().split('\n'); // Check for non urls and remove for (var i = 0; i < urls.length; i++) { if (urls[i].slice(0, 4) != 'http') { urls.splice(i, 1); } } // Iterate through the urls for (var i = 0; i < urls.length; i++) { var url = urls[i]; // request function is asynchronous, hence requirement for self-executing function // Cannot guarantee the execution order of the callback for each url, therefore save results to separate files request(url, ( function(url) { return function(err, resp, body) { if (err) throw err; // Extract the pg parameter (book page) from the url // We will use this to only extract the text from this book page // because a retrieved html page contains multiple book pages var pg = url.slice(url.indexOf('pg=') + 3, url.indexOf('&output=text')); // // Define the filename // var number = pg.slice(2, pg.length); var zeroes = 4 - number.length; // Insert leading zeroes for (var j = 0; j < zeroes; j++) { number = '0' + number; } var filename = pg.slice(0, 2) + number + '.txt'; // Add path to filename if (output) { if (!fs.existsSync(output)) fs.mkdirSync(output); filename = output + '/' + filename; } // Delete the file if it already exists if (fs.existsSync(filename)) fs.unlinkSync(filename); // Make the DOM available to jquery $ = cheerio.load(body); // Select the book page // Pages are contained within 'div' elements (where class='flow'), // each of which contains an 'a' element where id is equal to the page // Use ^ to match pages because sometimes page ids can have a trailing hyphen and extra characters var page = $('div.flow:has(a[id=' + pg + ']), div.flow:has(a[id^=' + pg + '-])'); // // Extract and save the text of the book page to the file // var hasText = false; // Text is in 'gtxt_body', 'gtxt_column' and 'gtxt_footnote' page.find('div.gtxt_body, div.gtxt_column, div.gtxt_footnote').each(function() { this.find('p.gtxt_body, p.gtxt_column, p.gtxt_footnote').each(function() { hasText = true; fs.appendFileSync(filename, this.text()); fs.appendFileSync(filename, '\n\n'); }); }); // Log progress if (hasText) { console.log("Retrieved and saved page: " + pg); } else { console.log("Skipping page: " + pg); } } } )(url)); } 
Возможно, мне придется изменить код так, чтобы он не принимал порядок gtxt_body и т. Д. Я только что наткнулся на страницу, где gtxt_footnote находится выше gtxt_column. user2661243 10 лет назад 0

Похожие вопросы