开发者

How can I scrape the current webpage with php/javascript?

I have made the following w开发者_如何学Cebpage for generating interactive todo lists: http://robert-kent.com/todo/todo.php

Basically, the user pastes a numbered todo list and each task is placed into it's own div with a unique id. Users can add notes to the tasks (done with javascript) and can click the green check when the task is done to hide it.

I'd like to add an Export button which would generate a report of which tasks were completed and which were not, along with the user-entered notes. After a bit of searching I understand that what I want to do is scrape the page, but I haven't the faintest idea of the best way to do it. Many of the articles and tutorials I have found with Google involve scraping other sites and don't really explain how I could iterate over each div on the page.

Full source here: http://pastebin.com/r7V3P5jK

Any suggestions?


The approach to providing an "Export" feature you've described is, unfortunately, completely wrong. Your page should submit back to the server with a parameter indicating that an "export" is desired. The server should then set the "Content-Disposition" header to something like attachment; filename="export.txt" (or whatever you want the filename to be).


If there is a div that contains the contents of what you want to export you could just simply use the innerHTML property to export the HTML code. For example, supposing the div had id of 'notes':

var notes = document.getElementById('notes');
alert(notes.innerHTML);

Now, how to get the user to actually save the actual HTML in a file? You'll need to either send it to a CGI or such on the server that sends back the content with the following header:

Content-Disposition: attachment; filename="export.txt"

Or, you'll have to use the methods on explained on this page: http://objectmix.com/javascript/314946-possible-generate-text-files-javascript.html. FYI: the methods explained on the page are not cross-browser compatible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜