PHP Magento 屏幕抓取

发布于 2024-10-10 16:09:23 字数 224 浏览 0 评论 0原文

我正在尝试抓取供应商 magento 网站,以节省一些时间,因为我需要收集大约 2000 种产品的信息。我完全可以为几乎任何事情编写屏幕抓取工具,但我遇到了一个主要问题。我使用 get_file_contents 来收集产品页面的 html。

问题是:

您需要登录才能查看产品页面。这是一个标准的 magento 登录,那么我该如何在屏幕抓取中解决这个问题呢?我不需要完整的脚本,只需要有关方法的建议。

I am trying to scrape a suppliers magento site in an effort to save some time because of there being around 2000 products I need to gather info for. I'm totally OK with writing a screen scraper for pretty much anything but i've encountered a major problem. Im using get_file_contentsto gather the html of the product page.

The problem is:

You need to be logged in, to view the product page. Its a standard magento login, so how can I get round this in my screen scraper? I don't require a full script, just advice on a method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怕倦 2024-10-17 16:09:23

使用 stream_context_create 您可以指定在以下情况下发送的标头:调用您的file_get_contents

我的建议是,打开浏览器并登录该网站。打开 Firebug(或您最喜欢的 Cookie 查看器)并获取 Cookie 并将其与您的请求一起发送。

编辑:这是来自 PHP.net 的示例:

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>

编辑(2):这超出了您的问题范围,但如果您想知道之后如何抓取网站,您可以查看 DOMDocument::loadHTML 方法。这本质上将为您提供所需的功能(即 XPath 查询,< a href="http://ca.php.net/manual/en/domdocument.getelementsbytagname.php" rel="nofollow noreferrer">getElementsByTagName, getElementsById) 来抓取您需要的内容。

如果您想抓取一些简单的内容,还可以将 RegEx 与 preg_match_all 一起使用。

Using stream_context_create you can specify headers to be sent when calling your file_get_contents.

What I'd suggest is, open your browser and login to the site. Open up Firebug (or your favorite Cookie viewer) and grab the cookies and send them with your request.

Edit: Here's an example from PHP.net:

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>

Edit (2): This is out of the scope of your question, but if you are wondering how to scrape the website afterwards you could look into the DOMDocument::loadHTML method. This will essentially give you the required functions (i.e. XPath query, getElementsByTagName, getElementsById) to scrape what you need.

If you want to scrape something simple, you can also use RegEx with preg_match_all.

优雅的叶子 2024-10-17 16:09:23

如果您熟悉 CURL,这应该在一天左右的时间内相对简单地完成。我创建了一些类似的应用程序来登录银行检索数据 - 当然这也需要身份验证。

下面是一个链接,其中包含如何使用带有 cookie 的 CURL 进行身份验证的示例:

http://coderscult.com/php/php-curl/2008/05/20/php-curl-cookies-example/

如果您可以抓取页面的输出,可以使用正则表达式解析您的结果。或者,您可以使用像 Snoopy 这样的类来为您完成这项工作:

http://sourceforge.net/projects/史努比/

If you're familiar with CURL this should be relatively simple to do in a day or so. I've created some similar apps to login to banks to retrieve data - which of course also require authentication.

Below is a link with an example of how to use CURL with cookies for authentication purposes:

http://coderscult.com/php/php-curl/2008/05/20/php-curl-cookies-example/

If you can grab the output of the page you can parse for your results with a regex. Alternatively, you can use a class like Snoopy to do this work for you:

http://sourceforge.net/projects/snoopy/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文