Solved

Getting text from Wikipedia

Posted on 2009-07-13
1
352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

615 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