简体   繁体   English

Laravel-是否可以从外部网站将数据保存到mySQL数据库?

[英]Laravel - Is it possible to save data to a mySQL database from a external website?

Lets say I found a website that has the following markup: 可以说我发现了一个具有以下标记的网站:

 <body> <div id="paper"> <div id="contentwrapper"> <div id="rightcontent"> <h1>1967-002A</h1> <p> <strong>NSSDCA/COSPAR ID:</strong> 1967-002A</p> <div class="twocol"> <div class="urone"> <h2>Description</h2> <p> This US Air Force photo surveillance satellite was launched from Vandenberg AFB aboard a Thor Agena D rocket. It was a KH-4A (Key Hole-4A) type satellite. The satellite had fair image quality. </p> </div> <div class="urtwo"> <h2>Alternate Names</h2> <ul> <li>02642</li> </ul> <h2>Facts in Brief</h2> <p> <strong>Launch Date:</strong> 1967-01-14 <br/> <strong>Launch Vehicle:</strong> Thor <br/> <strong>Launch Site:</strong> Vandenberg AFB, United States <br/> <strong>Mass:</strong> 1500.0 kg <br/> </p> <h2>Funding Agency</h2> <ul> <li>Department of Defense-Department of the Air Force (United States)</li> </ul> <h2>Discipline</h2> <ul> <li>Surveillance and Other Military</li> </ul> </div> </div> </div> </div> </div> </body> 

This contains information, such as Description, Launch Date, Launch Vehicle, Launch Site and Mass, Funding Agency and Discipline. 其中包含信息,例如“说明”,“发射日期”,“发射工具”,“发射地点和质量”,“资助机构和纪律”。 - These could all be the columns in the mySQL database. -这些都可能是mySQL数据库中的列。

The webpage has a link of /spacecraftDisplay.do?id=1967-002A . 该网页具有/spacecraftDisplay.do?id=1967-002A的链接。 I already have a database with the 1967-002A - the spacecraft identifier. 我已经有了一个带有1967-002A的数据库-航天器标识符。 So I am guessing to take each identifier from my database and save the data from the URL with the same identifier. 所以我想从数据库中获取每个标识符,并使用相同的标识符保存URL中的数据。 Each webpage is the same 每个网页都相同

I already know how to save data from an external API which has a JSON format using Guzzle. 我已经知道如何使用Guzzle从具有JSON格式的外部API保存数据。 Instead of JSON, we are dealing with the HTML of the external website. 我们正在处理外部网站的HTML,而不是JSON。

What I want to know first, if its possible to even save this data from the webpage or is there limitations to what you can do? 我首先想知道的是,是否有可能甚至可以从网页上保存此数据,或者您可以做些什么?

You can go with a DOM parser http://simplehtmldom.sourceforge.net/ 您可以使用DOM解析器http://simplehtmldom.sourceforge.net/

It basically sticks an entire HTML page in an object then you can access any element from that object. 它基本上将整个HTML页面粘贴在一个对象中,然后您可以访问该对象中的任何元素。

//Example
$html = file_get_html('http://www.google.com/');
foreach($html->find('img') as $element) {
    echo $element->src . '<br>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM