0

I tried to create a scraper for bestbuy.com but it seems that PHP can't scrape BestBuy.

To test, I tried the same piece of code on amazon and other pages and it returns the page title. Which means that the code works. Yet, BestBuy can't be accessed with PHP.

This is the code I used to get the title:

$content = file_get_contents('https://www.bestbuy.com/');
$select= preg_match("/<title>(?<title>.+)<\/title>/m",$content,$title);
echo $title['title'];

I was expecting it to output BestBuy website title but it gave this error:

Warning: file_get_contents(https://www.bestbuy.com/): Failed to open stream: HTTP request failed! in C:\xampp\htdocs\internshipPHP\Assignment#4\scraper.php on line 3

Do you have any idea?

  • Perhaps it's blocked from your IP address or something. – ADyson Mar 06 '22 at 18:01
  • Did you try my code on your pc and it worked? Because I think it doesn't work also on other IP addresses. – Omar Alsafarti Mar 06 '22 at 18:03
  • Or try checking response headers for details and clues. See also https://stackoverflow.com/questions/697472/php-file-get-contents-returns-failed-to-open-stream-http-request-failed and several others, if you google the error message – ADyson Mar 06 '22 at 18:04
  • Hey you got a point, I just tried it, it timed out. Interesting. Possibly because it's expecting a "user-agent". It's worth looking into. – GetSet Mar 06 '22 at 18:24

0 Answers0