I want to fetch div content from a webpage and to use it in my page.
I have the url http://www.freebase.com/search?limit=30&start=0&query=cancer
I want to fetch div content with id artilce-1001. How can I do that in php or jQuery?
I want to fetch div content from a webpage and to use it in my page.
I have the url http://www.freebase.com/search?limit=30&start=0&query=cancer
I want to fetch div content with id artilce-1001. How can I do that in php or jQuery?
In PHP you'll probably want to GET the page (probably using CURL or similar) then you'll have to parse the html, which is probably not the easiest thing to do, but I'm guessing there are libraries out there to help you with that.
If you want to use PHP, you may want to have a look at Simple HTML DOM. It is a nice single include file. The docs give an example of scraping slashdot as:
Regex is never any good at (and should never be used for) parsing HTML. It isn't regular, and you end up with huge regular expressions for what would be simple in jQuery or the above library
EDIT:
So you would want to use something like
PHP is server-side, jQuery is client side so it really depends on what you want to achieve. Also note that because of the same-origin policy, you generally can't perform an Ajax request to another domain via javascript anyway (but you could proxy it via your own server)
jQuery aside, here's a simple way to do it in PHP, which will work for the case you provide
Note the 's' modifier, which makes . match newlines, and the .*? idiom, which makes the matching the inner part non-greedy so that it only eats up the next
</div>
This works for your case, but regexes are generally ill suited to this task. You could load the HTML into a DOmDocument and access it that way.
PHP:
Regular expression probably won't work, but it's example or direction you can use, just play with it :)
If this really is about a Freebase topic and not about getting HTML from a website in general, using the API and getting familiar with MQL should be the better solution since that would allow you to restrict your search in specific types easily.
Example:
Can be passed to mqlread directly and returns a JSON list with possible matches for the astrological sign "Cancer". Then, you can simply get the article and image by using trans_raw and/or trans_blurb, if you need to. :)
Use the following
There is an example like this on the jQuery site here