Solved

Getting text from Wikipedia

Posted on 2009-07-13
1
342 Views
Last Modified: 2012-05-07
I have a database of products, there is a column called "wikiurl" which relates to the Wikipedia URL. My question is what is the best way to extract the intro paragraph from wikipedia. So for example if "wikiurl" = "iPhone" then I would want to get the first paragraph from the page: http://en.wikipedia.org/wiki/Iphone

I'm using PHP and CodeIgniter. Whats the best way to scrape this info?
0
Comment
Question by:alex_wareing
1 Comment
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24845065
The code below seems to do what you want. It fetches the first paragraph from the page. I am not sure if this will work with all articles.

Some warnings are generated during the parsing, which is why I used error_reporting() to supress them.
error_reporting(E_ALL^E_WARNING);
$d = new DOMDocument();
$d->loadHTMLFile('http://en.wikipedia.org/wiki/Iphone');
$paras = $d->getElementsByTagName('p');
echo $paras->item(0)->nodeValue;

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question