【问题标题】:Web Scraping : Getting Specific Values in a WebsiteWeb Scraping : 在网站中获取特定值
【发布时间】:2014-02-26 08:07:45
【问题描述】:

您好,我想了解一下这个网站的一些信息 http://www.greenbook.org/market-research-companies/united-states-of-america

我想获取所有公司名称和电话号码

起初我试图获取所有数据(只是为了尝试)

使用此代码

<?php
require_once 'simple_html_dom.php';
$html = file_get_html('http://www.greenbook.org/market-research-companies/united-states-of-america');

foreach($html->find('h2') as $element){
  echo $element->innertext . '<br>'; 
}
?>

但它不起作用,任何人都可以帮我解决这个问题,我怎样才能在此处获得公司列表

【问题讨论】:

  • 我想知道如果没有跨域访问,这是否可能。
  • 我在 URL 源中没有看到任何 H2 元素。
  • Pradeep 有自称美国市场研究公司
  • 似乎网络抓取无法与greenbook.org 一起使用;也许页面是用 javascript 或其他东西生成的,但简单的 dom 返回一个cant find page..
  • @vlzvl no 我可以在浏览器中禁用 JavaScript 后获取所有信息

标签: javascript php jquery web web-scraping


【解决方案1】:

完美运行的代码:

<?php

set_time_limit(0);

function get_curl_output($link)
{
    $channel = curl_init();
    curl_setopt($channel, CURLOPT_URL, $link);
    curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($channel, CURLOPT_CONNECTTIMEOUT, 4);
    curl_setopt($channel, CURLOPT_TIMEOUT, 120);
    curl_setopt($channel, CURLOPT_VERBOSE, true);
    curl_setopt($channel, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219');
    curl_setopt($channel, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($channel, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

    $output = curl_exec($channel);
    curl_close($channel);
    return $output;
}

$content = get_curl_output('http://www.greenbook.org/market-research-companies/united-states-of-america');

$article_count = substr_count($content, '<article class="article article-lrg ">');
$article_data = array();
$company_data = array();

function article_getter($content, $index = 0)
{
    $article_pos_1 = strpos($content, '<article class="article article-lrg ">', $index);
    $article_pos_2 = strpos($content, '</article>', $article_pos_1+38);
    $article_str = substr($content, $article_pos_1+38, ($article_pos_2-38)-$article_pos_1);

    return array($article_str, $article_pos_2);
}

for ($i = 0; $i < $article_count; $i++)
{
    if ( $i === 0 )
    {
        $article_data[$i] = article_getter($content);
    }
    else
    {
        $article_data[$i] = article_getter($content, $article_data[$i - 1][1]);
    }       
}

function filter($article)
{
    $name_pos_1 = strpos($article, '<b>');
    $name_pos_2 = strpos($article, '</b>', $name_pos_1+3);
    $name = substr($article, $name_pos_1+3, ($name_pos_2-3)-$name_pos_1);

    $telephone_pos_1 = strpos($article, '<span class="strong">Telephone:</span>', $name_pos_2);
    $telephone_pos_2 = strpos($article, '&nbsp;<span', $telephone_pos_1+38);
    $telephone = substr($article, $telephone_pos_1+38, ($telephone_pos_2-38)-$telephone_pos_1);
    $telephone = trim($telephone);

    return array($name, $telephone);
}

for ($i = 0; $i < count($article_data); $i++)
{ 
    $company_data[$i] = filter($article_data[$i][0]);
}

var_dump($company_data); //do whatever you want this array

?>

【讨论】:

  • 执行此操作时是否需要使用 mozilla 作为浏览器?
  • 致命错误:C:\xampp\htdocs\try\index.php 第 15 行的最大执行时间超过 30 秒
  • 我还有一个问题
  • @SolutionsResource 有什么问题?
  • 我需要先获取网址,然后从该网址获取公司名称、电话号码、电子邮件地址和联系人
猜你喜欢
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2018-11-13
相关资源
最近更新 更多