This question already has answers here:
What's the best way of scraping data from a website? [closed]
(2 answers)
Closed 4 years ago.
I would like to scrape the information of the product with PHP like description, title, price, quantity etc with a use of the url of the product in the amazon website or with the use on the ISBN.
How should I do it? Is there any example of code for me?
Thanks!
With PHP you can get the job done by opening the url with a file_get_contents() and by using a preg_match() with regex.
Then, taking a random article page and browsing its html code, for example https://www.amazon.com/Learning-PHP-MySQL-JavaScript-jQuery/dp/1491978910/
By inspecting the code we can see the price is in a specific <span>:
<span class="a-size-medium a-color-price header-price">
$35.00
</span>
Then we can do something like:
$link = 'https://www.amazon.com/Learning-PHP-MySQL-JavaScript-jQuery/dp/1491978910/';
$page_content = file_get_contents($link);
if(preg_match('/<span class=\"a-size-medium a-color-price header-price\">(.*?)<\/span>/i',
$page_content, $matches)) {
$price = trim($matches[1]);
} else {
echo "Price not found.";
$price = 0;
}
Then you have here enough basis to repeat the operation to get all the other informations you need to fetch.
Notice that it's not the best relevent way to do this as it assumes the HTML patterns are always the same from an article to another. The best relevant way is to use their API if possible.
Related
This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 8 years ago.
I want to extract site link from Google URL, I need an efficient way to do this,
I have extracted this, but i am not comfortable with that like,
$googleURL = "http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/about%3Fgl%3DCA%26hl%3Den-CA&ved=0CHAQlQU&sa=X&ei=HzrCVNX-JqSzigb-94D4CQ&s=ANYYN7nQx_FiR1PuowDmXBi1oyfkI2MImg";
I want this
https://plus.google.com/110334461338830338847/
I have done this in a following way.
$first = current(explode("about", $googleURL)); // returns http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/
and then,
$myLink = explode("&q=", $first);
echo $myLink[1]; // return my need => https://plus.google.com/110334461338830338847/
but there may be two "about" or "&q=" in a googleURL which can cause problem.
I know that, this googleURL will be redirected to my need, but I need that specific link for a purpose.
I guess that it is not really safe to parse that since google can change its implementation anytime.
However, if you want to get a parameter from a String url, this question covers it pretty well :
How to get parameters from a URL string?
$parts = parse_url($googleUrl);
parse_str($parts['query'], $query);
echo $query['q'];
This question already has answers here:
how to use dom php parser
(4 answers)
Closed 9 years ago.
<?php
$html = file_get_contents('http://xpool.xram.co/index.cgi');
echo $html;
?>
I want to get information in a tag on a remote web site using php. and only the tags.
I found this small string that is great for retrieving the entire site source. However, i want to get a small section only. How can I filter out all the other tags and get only the one tag I need?
I'd suggest using a PHP DOM parser. (http://simplehtmldom.sourceforge.net/manual.htm)
require_once ('simple_html_dom.php');
$html = file_get_contents('http://xpool.xram.co/index.cgi');
$p = $html->find('p'); // Find all p tags.
$specific_class = $html->find('.classname'); // Find elements with classname as class.
$element_id = $html->find('#element'); // Find element with the id element
Read the docs, there are tons of other options available.
This question already has answers here:
Grabbing the href attribute of an A element
(10 answers)
Closed 9 years ago.
I'm new to php and and i need help with something.
I need to divide the image and the content from this variable. It has a image and the description.
$content = "<a href="/pin/211106301253428599/">
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a>
No matter where or why you travel, there's always something wonderfully new to be found! #quote";
I know this is something simple but please help me. I need the image in a variable and the content with the link in another..
Thanks.
As #Barmar says, you should use a DOM parsing library.
You may find very useful this: http://simplehtmldom.sourceforge.net
Its use is similar to a jQuery parsing, and you only have to read doc to know how to use it (very good examples => http://simplehtmldom.sourceforge.net/manual.htm)
Example:
$content = "<a href="/pin/211106301253428599/">
<img src="http://media-cache-ak0.pinimg.com/192x/8d/97/f5/8d97f59de2c2d5d8d83fa61f1f4ad7a5.jpg"></a>
No matter where or why you travel, there's always something wonderfully new to be found! #quote";
$html = str_get_html($content);
$image = $html->find('img');
$links = $html->find('a');
And you'll have what you want in $image and $links, :D
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have this link
http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1
That gives me the latest video from GudjoDaneel but I'd like to print our this title inside a PHP file
<title type='text'>The GD Project S3 | NEVER GIVE UP! | Division 1</title><content type='text'>
I'd appreciate it if someone could help me where to begin. And what I could look up.
I'd suggest looking up SimpleXML. It's easy to use once you get the hang of it, and you can get the title in just four line:
$url = 'http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1';
$source = file_get_contents($url);
$xml = new SimpleXMLElement($source);
$title = $xml->entry->title;
Do note, though, that $title is a PHP object in this case. If you echo it straight away, it'll be reinterpreted as a string, and everything will be alright. If you plan on doing anything else with it, you'll need to cast it as a string, like this:
$title = strval($title);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Linkify PHP text
Hi I have a field from a database that contains http links as well as normal text.
I need to find out a way to display this information using PHP to show Text and a link when applicable.
the following is my sql query:
$sql =
"select bw_supporting_doc
from cm3rm2 with(nolock)
where number = '$id'";
the output of this sql statement is:
Please find below the link to the FSS: http://blahblack.html
I need to display the http://blahblack.html as a link at the moment it is displaying as normal text.
Thanks!
function hyperlink($text){
$text = preg_replace("#(http://)+?([.]?[a-zA-Z0-9_/-])*#", "\\0", $text);
return $text;
}
this might do the job
example usage:
$str = 'this is a random string which contains a link http://www.yahoo.com';
echo hyperlink($str);