dongle0396 2011-01-09 21:57
浏览 35
已采纳

如何修改href链接(scraper)

I'm scraping a page, however before my content is echoed I would like to edit the link.

What is the best way to do this?

I'm currently using Simple HTML DOM Parser:

// create HTML DOM
$html = file_get_html('http://myurl.com');
// remove all image
  foreach($html->find('img') as $e)
 $e->outertext = '';
foreach($html->find('font') as $e)
$e->outertext = '';
// find all td tags with attribite align=center
foreach($html->find('td[align=left]', 2) as $e)
   echo $e->innertext;

There is this bit in one of the URLs:

<a target="retailer" href="/cgi-bin/redirect.cgi?name=Storm%20Computers&amp;linkid=2&amp;newurl=http%3A%2F%2Fwww.stormcomputers.com.au%2Fcatalog%2Findex.php%3FcPath%3D38_364&amp;query=sandy%20bridge&amp;uca=208-0-0&amp;kwi=&amp;rpos=2" title="Storm Computers: Click to see item">$149.00</a>

I would like to change this to

<a href="http%3A%2F%2Fwww.stormcomputers.com.au%2Fcatalog%2Findex.php%3FcPath%3D38_364&amp;query=sandy%20bridge&amp;uca=208-0-0&amp;kwi=&amp;rpos=2">$149.00</a>

(ie. just after &newurl=)

Thanks.

  • 写回答

3条回答 默认 最新

  • duande9301 2011-01-09 23:39
    关注

    I'm not familiar with the parser you're using, but something like this might work:

    foreach ($html->find('a') as $link) {
        $urlparts = parse_url($link->href);
        $query = parse_str($urlparts['query'], $params);
    
        if (isset($params['newurl'])) {
            $link->href = $params['newurl'];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表