php抓取数据并且保存到Excel

<?php
//获取网页内容
require './lib/PHPExcel-1.8/Classes/PHPExcel.php';
set_time_limit(0);
//$arr = 'Arc,Arctic Breath,Assassin\'s Mark,Ball Lightning,Blight,Bodyswap,Bone Offering,Clarity,Cold Snap,Conductivity,Contagion,Conversion Trap,Convocation,Dark Pact,Despair,Discharge,Discipline,Elemental Weakness,Enfeeble,Essence Drain,Fire Nova Mine,Fireball,Firestorm,Flame Dash,Flame Surge,Flameblast,Flammability,Flesh Offering,Freezing Pulse,Frost Bomb,Frost Wall,Frostbite,Frostbolt,Glacial Cascade,Herald of Thunder,Ice Nova,Ice Spear,Incinerate,Kinetic Blast,Lightning Spire Trap,Lightning Tendrils,Lightning Trap,Lightning Warp,Magma Orb,Orb of Storms,Power Siphon,Purity of Elements,Purity of Lightning,Raise Spectre,Raise Zombie,Righteous Fire,Scorching Ray,Shock Nova,Siphoning Trap,Spark,Spirit Offering,Storm Burst,Storm Call,Summon Chaos Golem,Summon Holy Relic,Summon Lightning Golem,Summon Raging Spirit,Summon Skeleton,Tempest Shield,Vaal Arc,Vaal Blight,Vaal Clarity,Vaal Cold Snap,Vaal Discipline,Vaal Fireball,Vaal Flameblast,Vaal Ice Nova,Vaal Impurity of Lightning,Vaal Lightning Trap,Vaal Power Siphon,Vaal Righteous Fire,Vaal Spark,Vaal Storm Call,Vaal Summon Skeletons,Vortex,Wither,Wrath';
//$arr = 'Animate Weapon,Arctic Armour,Barrage,Bear Trap,Blade Flurry,Blade Vortex,Bladefall,Blast Rain,Blink Arrow,Blood Rage,Burning Arrow,Caustic Arrow,Charged Dash,Cremation,Cyclone,Desecrate,Detonate Dead,Double Strike,Dual Strike,Elemental Hit,Ethereal Knives,Explosive Arrow,Explosive Trap,Fire Trap,Flamethrower Trap,Flicker Strike,Freeze Mine,Frenzy,Frost Blades,Grace,Haste,Hatred,Herald of Agony,Herald of Ice,Ice Shot,Ice Trap,Lacerate,Lightning Arrow,Lightning Strike,Mirror Arrow,Phase Run,Poacher\'s Mark,Projectile Weakness,Puncture,Purity of Ice,Rain of Arrows,Reave,Riposte,Scourge Arrow,Seismic Trap,Shrapnel Shot,Siege Ballista,Smoke Mine,Spectral Shield Throw,Spectral Throw,Split Arrow,Summon Ice Golem,Temporal Chains,Tornado Shot,Toxic Rain,Unearth,Vaal Blade Vortex,Vaal Burning Arrow,Vaal Cyclone,Vaal Detonate Dead,Vaal Double Strike,Vaal Grace,Vaal Haste,Vaal Impurity of Ice,Vaal Lightning Strike,Vaal Rain of Arrows,Vaal Reave,Vaal Spectral Throw,Viper Strike,Volatile Dead,Whirling Blades,Wild Strike';
//$arr = 'Abyssal Cry,Ancestral Protector,Ancestral Warchief,Anger,Animate Guardian,Cleave,Consecrated Path,Decoy Totem,Determination,Devouring Totem,Dominating Blow,Earthquake,Enduring Cry,Flame Totem,Glacial Hammer,Ground Slam,Heavy Strike,Herald of Ash,Herald of Purity,Ice Crash,Immortal Call,Infernal Blow,Leap Slam,Molten Shell,Molten Strike,Punishment,Purity of Fire,Rallying Cry,Reckoning,Rejuvenation Totem,Searing Bond,Shield Charge,Shockwave Totem,Smite,Static Strike,Summon Flame Golem,Summon Stone Golem,Sunder,Sweep,Tectonic Slam,Vaal Ancestral Warchief,Vaal Earthquake,Vaal Glacial Hammer,Vaal Ground Slam,Vaal Impurity of Fire,Vaal Molten Shell,Vengeance,Vigilant Strike,Vitality,Vulnerability,Warlord\'s Mark';
//$arr = 'Apprentice Cartographer\'s Sextant,Armourer\'s Scrap,Blacksmith\'s Whetstone,Blessed Orb,Blessing of Chayula,Blessing of Esh,Blessing of Tul,Blessing of Uul-Netol,Blessing of Xoph,Cartographer\'s Chisel,Chaos Orb,Chromatic Orb,Divine Orb,Exalted Orb,Gemcutter\'s Prism,Glassblower\'s Bauble,Jeweller\'s Orb,Journeyman Cartographer\'s Sextant,Master Cartographer\'s Sextant,Mirror of Kalandra,Orb of Alchemy,Orb of Alteration,Orb of Annulment,Orb of Augmentation,Orb of Chance,Orb of Fusing,Orb of Regret,Orb of Scouring,Orb of Transmutation,Portal Scroll,Regal Orb,Scroll of Wisdom,Silver Coin,Vaal Orb,Ancient Orb,Engineer\'s Orb,Harbinger\'s Orb,Mirror Shard,Orb of Binding,Orb of Horizons,Perandus Coin,Exalted Shard,Eternal Orb';
//$arr = 'Sacrifice at Dusk,Sacrifice at Midnight,Sacrifice at Dawn,Sacrifice at Noon,Mortal Grief,Mortal Rage,Mortal Hope,Mortal Ignorance,Eber\'s Key,Yriel\'s Key,Inya\'s Key,Volkuur\'s Key,Fragment of the Hydra,Fragment of the Phoenix,Fragment of the Minotaur,Fragment of the Chimera,Sacrifice Set,Mortal Set,Pale Court Set,Shaper Set,Xoph\'s Breachstone,Tul\'s Breachstone,Esh\'s Breachstone,Uul-Netol\'s Breachstone,Chayula\'s Breachstone,Offering to the Goddess,Divine Vessel,Ancient Reliquary Key,Timeworn Reliquary Key';
//$arr = 'Anatomical Knowledge,Ancient Waystones,Apparitions,Assassin\'s Haste,Atziri\'s Reign,Blood Sacrifice,Brawn,Brittle Barrier,Brute Force Solution,Careful Planning,Cheap Construction,Chill of Corruption,Clear Mind,Coated Shrapnel,Cold Steel,Collateral Damage,Combat Focus_(Cobalt Jewel),Combat Focus_(Crimson Jewel),Combat Focus_(Viridian Jewel),Combustibles,Conqueror\'s Efficiency,Conqueror\'s Longevity,Conqueror\'s Potency,Corrupted Energy,Dead Reckoning,Efficient Training,Eldritch Knowledge,Emperor\'s Cunning,Emperor\'s Mastery,Emperor\'s Might,Emperor\'s Wit,Energised Armour,Energy From Within,Fertile Mind,Fevered Mind,Fight for Survival,Fireborn,First Snow,Fluid Motion,Fortified Legion,Fragile Bloom,Fragility,From Dust,Frozen Trail,Grand Spectrum_(Crimson Jewel),Grand Spectrum_(Viridian Jewel),Grand Spectrum_(Cobalt Jewel),Growing Agony,Hair Trigger,Hazardous Research,Healthy Mind,Hidden Potential,Hotfooted,Hungry Abyss,Inertia,Inevitability,Inspired Learning,Intuitive Leap,Izaro\'s Turmoil,Lioneye\'s Fall,Malicious Intent,Mantra of Flames,Martial Artistry,Might and Influence,Might in All Forms,Might of the Meek,Mutated Growth,Omen on the Winds,Overwhelming Odds,Pacifism,Pitch Darkness,Poacher\'s Aim,Powerlessness,Primordial Eminence,Primordial Harmony,Primordial Might,Pugilist,Pure Talent,Rain of Splinters,Rapid Expansion,Reckless Defence,Ring of Blades,Rolling Flames,Sacrificial Harvest,Self-Flagellation,Shattered Chains,Soul\'s Wick,Spire of Stone,Spirit Guards,Spirited Response,Spreading Rot,Static Electricity,Steel Spirit,Sudden Ignition,Survival Instincts,Survival Secrets,Survival Skills,Tempered Flesh,Tempered Mind,Tempered Spirit,The Anima Stone,The Blue Dream,The Blue Nightmare,The Golden Rule,The Green Dream,The Green Nightmare,The Long Winter,The Red Dream,The Red Nightmare,The Vigil,To Dust,Transcendent Flesh,Transcendent Mind,Transcendent Spirit,Unending Hunger,Unnatural Instinct,Unstable Payload,Vaal Sentencing,Violent Dead,Volley Fire,Warlord\'s Reach,Watcher\'s Eye,Weight of Sin,Weight of the Empire,Wildfire,Winter Burial,Winter\'s Bounty';
$arr = 'Rigwald\'s Savagery,United in Dream,Soulwrest,Void Battery,Voidforge,Atziri\'s Disfavour,The Searing Touch,Windripper,Starforge,The Poet\'s Pen,Fate of the Vaal,Taryn\'s Shiver,Rigwald\'s Command,Nebuloch,The Rippling Thoughts,Silverbranch,The Dancing Duo,Kingmaker,Disintegrator,Mjölner,Tidebreaker,Corona Solaris,Kitava\'s Feast,Brutus\' Lead Sprinkler,Doryani\'s Catalyst,Ephemeral Edge,The Searing Touch,Cameria\'s Avarice,The Tempest,Cybil\'s Paw,Lioneye\'s Glare,Arborix,Breath of the Council,Dreadbeak,Cospri\'s Malice,Chaber Cairn,Death\'s Opus,Shimmeron,Story of the Vaal,The Goddess Scorned,Varunastra,Queen\'s Escape,Soul Taker,Moonsorrow,Uul-Netol\'s Embrace,Wings of Entropy,Dreadsurge,Grelwood Shank,Martyr of Innocence,Sanguine Gambol,Shade of Solaris,The Cauteriser,Divinarius,Hand of Wisdom and Action,The Enmity Divine,Ahn\'s Might,The Stormwall,The Gryphon,Trypanon,Severed in Sleep,Ichimonji,Mirebough,Doomfletch\'s Prism,Arakaali\'s Fang,Jorrhast\'s Blacksteel,The Harvest,Geofri\'s Devotion,The Scourge,Silverbough,Bloodseeker,Agnerod West,Xoph\'s Nurture,Beltimber Blade,Xirgil\'s Crank,Reach of the Council,Agnerod North,Agnerod South,Augyre,Hrimnor\'s Dirge,Chin Sol,Death\'s Hand,Razor of the Seventh Sun,Tulfall,Agnerod East,Hopeshredder,Ngamahu\'s Flame,Slivertongue,Hegemony\'s Era,Dying Breath,Doon Cuebiyari,Realm Ender,Touch of Anguish,The Tempestuous Steel,Allure,Amplification Rod,Apep\'s Rage,Balefire,Bino\'s Kitchen Knife,Brain Rattler,Brightbeak,Chitus\' Needle,Dreamfeather,Dying Breath,Edge of Madness,Hand of Thought and Motion,Hiltless,Izaro\'s Dilemma,Kongor\'s Undying Rage,Lioneye\'s Glare,Pledge of Hands,Quill Rain,Singularity,Terminus Est,The Consuming Dark,The Dark Seer,The Supreme Truth,Uul-Netol\'s Kiss,Vulconus,Xoph\'s Inception,Essentia Sanguis,Innsbury Edge,The Wasp Nest,Piscator\'s Vigil,Oro\'s Sacrifice,Lifesprig,Spine of the First Claimant,Essentia Sanguis,Midnight Bargain,Jack,Callinellus Malleus,The Goddess Bound,The Princess,Hezmana\'s Bloodlust,Sinvicta\'s Mettle,Taryn\'s Shiver,Mon\'tregul\'s Grasp,Null\'s Inclination,Clayshaper,Ephemeral Edge,Kondo\'s Pride,Voltaxic Rift,Hyaon\'s Fury,Doomsower,Tulborn,Advancing Fortress,Al Dhih,Aurumvorax,Axiom Perpetuum,Cameria\'s Maul,Cane of Unravelling,Chober Chaber,Cybil\'s Paw,Daresso\'s Passion,Darkscorn,Death\'s Harp,Debeon\'s Dirge,Dreadarc,Dyadus,Earendel\'s Embrace,Eclipse Solaris,Femurs of the Saints,Fencoil,Fidelitas\' Spike,Flesh-Eater,Frostbreath,Geofri\'s Baptism,Gorebreaker,Goredrill,Heartbreaker,Hrimnor\'s Hymn,Infractem,Kaom\'s Primacy,Lakishu\'s Blade,Lavianga\'s Wisdom,Limbsplit,Mark of the Doubting Knight,Marohi Erqi,Mightflay,Moonbender\'s Wing,Moonsorrow,Mortem Morsu,Nuro\'s Harp,Nycta\'s Lantern,Obliteration,Ornament of the East,Pillar of the Caged God,Prismatic Eclipse,Realmshaper,Reaper\'s Pursuit,Rebuke of the Vaal,Redbeak,Relentless Fury,Reverberation Rod,Rigwald\'s Charge,Rive,Scaeva,Silverbranch,Sire of Shards,Storm Cloud,Storm Prison,Taproot,The Blood Reaper,The Dancing Dervish,The Screaming Eagle,The Searing Touch,The Stormheart,The Whispering Ice,Tremor Rod,Twyzel,Ungil\'s Gauche,White Wind,Wideswing,Widowmaker,Wildslash,Wings of Entropy,Abberath\'s Horn,Ewar\'s Mirage,Queen\'s Decree,Shiversting,The Blood Thorn,Doomfletch,Ashcaller,Quecholli,Last Resort,Roth\'s Reach,Iron Commander,Bitterdream,Voidhome,Bloodplay';

$arr = str_replace(' ','_',$arr);
$arr = explode(',',$arr);
$objPHPExcel = new PHPExcel(); //实例化一个PHPExcel()对象
$objSheet = $objPHPExcel->getActiveSheet(); //选取当前的sheet对象
$objSheet->setTitle('helen'); //对当前sheet对象命名
$array = [];
foreach ($arr as $v) {
    $str = '';

    $url="https://xxxxxxxxxx.com/$v";
    $ch = curl_init();

    $timeout = 10;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $str = curl_exec($ch);

//删除 <div class="chapter">之前的内容
    //$test1 = '<span class="infobox-page-container"><span class="item-box -gem"><span class="header -single">';
    $test1 = '<span class="infobox-page-container"><span class="item-box -unique"><span class="header -double">';
    if ($x = strpos($str, $test1)) $str = substr($str, $x);

//删除 <!-- 章节列表 结束 -->之后的内容
    preg_match("/<img\b\salt.*? (width=\"\w*\" height=\"\w*\".*?)>/",$str,$jie);
    if ($x = strpos($str, $jie[1])) $str = substr($str, 0, $x);
    /*if ($x = strpos($str, ' width="78" height=')) $str = substr($str, 0, $x);
    if ($x = strpos($str, ' width="156" height=')) $str = substr($str, 0, $x);
    if ($x = strpos($str, ' width="156" height="156" /></a></span><span')) $str = substr($str, 0, $x);
    if ($x = strpos($str, 'width="156" height="234" /></a></span><span')) $str = substr($str, 0, $x);
    if ($x = strpos($str, ' width="78" height="234" /></a></span><span')) $str = substr($str, 0, $x);
    if ($x = strpos($str, 'width="78" height="78" srcset=')) $str = substr($str, 0, $x);*/

    if ($x = strpos($str, '</span></span><a href="/File')) $strings = substr($str, 0, $x);
//输出结果
//preg_match("/\bsrc\b\s*=\s*[\'\"]?([^\'\"]*)[\'\"]?/i", $str, $a);
//print_r($a);

//echo '<link rel="stylesheet" href="https://xxxxxxx.com/load.php?debug=false&amp;lang=en&amp;modules=site.styles&amp;only=styles&amp;skin=hydradark">'.$str.'</em></span></span></span></span>';
//echo $str.' width="78" height="78" /></a></span>';

//3.使用fopen
    //echo $str;exit();
    preg_match_all("/\bsrc\b\s*=\s*[\'\"]?([^\'\"]*)[\'\"]?/i", $str, $a);
    if (isset($a[1][1])) {
        $img = $a[1][1];
    } else {
        $img = $a[1][0];
    }
    curl_close($ch);
    sleep(3);
    //echo $img;
    $path = './images/';

    ob_clean();
    ob_start();
    readfile($img);		//读取图片
    $img = ob_get_contents();	//得到缓冲区中保存的图片
    ob_end_clean();		//清空缓冲区
    $fp = fopen($path.'POE_Unique_Weapons_'.strtolower($v).'.png','w+');	//写入图片
    if(fwrite($fp,$img))
    {
        fclose($fp);
        echo "图片保存成功";
    }else{
        echo 2222;
    }
    $strings = str_replace('<br />','------',$strings);
    $strings = str_replace('</span>','||||||',$strings);
    $strings = strip_tags($strings);
    $strings = str_replace('------','<br >',$strings);
    $strings = str_replace('||||||','<hr >',$strings);

    $value = str_replace('_',' ',$v);

    $x = strpos($strings, '<br >');
    $tag = substr($strings, 0, $x);
    $tag = str_replace('<hr >',',',$tag);

    $strings = str_replace("$value<hr >",'',$strings);

    $img =  '/uploads/20181125/POE_Unique_Weapons_'.strtolower($v).'.png';
    $title = str_replace("_",' ',$v);
    $array[] = [$title,'G','',$strings,$img,$tag];

}

$objSheet->fromArray($array); //利用fromArray()直接一次性填充数据
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007'); //设定写入excel的类型
$objWriter->save('./unique_weapons.xlsx');
?>

猜你喜欢

转载自blog.csdn.net/qq_42572322/article/details/84547032