du5591 2017-06-10 14:56
浏览 39
已采纳

修复php图像刮刀代码,以便在不同情况下更灵活

I was able to construct some code that grabs an image from the below website where the image link will be random every time, and mirrors it on another site. While it's great that this works, I'm unable to copy this format onto any other site. I see that the image is being grabbed with a getElementbyId "file," but from the original source code there are many many refrences to "file," so I'm a bit stuck. very knew to php still.

What I'm trying to be able to do, is replicate the below result, but on any site with a particular image.

 <?php
$html = 
file_get_contents("http://commons.wikimedia.org/wiki/Special:Random/File");
$dom = new DOMDocument();
$dom->loadHTML($html);
$remoteImage = $dom->getElementById("file")->firstChild->attributes[0]-
>textContent;
header("Content-type: image/png");
header('Content-Length: ' . filesize($remoteImage));
echo file_get_contents($remoteImage);
?>

Trying to figure out how I could reproduce that on this site for ex https://pokemondb.net/pokedex/wartortle

where I'm trying to pull the wartortle.jpg

My initial idea if not knowing exactly what the image would hypothetically be named, since I want this to work during random conditions, is to identify the image with it's tag < div class="colset">

Alas, plugging in "colset" instead of "file" didn't do the trick though.

Any thoughts?? Thanks so much.-Wilson

  • 写回答

1条回答 默认 最新

  • download92000 2017-06-11 06:24
    关注

    Using XPath is always a lot more flexible (although probably slower than other solutions). Using the previous example you could use the following to get the file name...

    <?php
    ob_start();
    $doc = new DOMDocument;
    
    $doc->loadHTMLFile('https://pokemondb.net/pokedex/wartortle');
    
    $xpath = new DOMXPath($doc);
    
    $query = "//li[@id='svtabs_basic_8']//img/@src";
    ob_end_clean();
    header('content-type: image/jpeg');
    $entries = $xpath->query($query);
    foreach ($entries as $entry) {
        readfile((string)$entry->value);
    }
    

    I've added the ob_start and ob_end_clean to remove the xml validation errors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp