【发布时间】:2023-03-17 14:30:02
【问题描述】:
我希望能够从 yahoo Finance 获取文章列表等数据。目前我有一个本地托管网页,在 yahoo Finance 中搜索 股票代码(例如 Nok),然后返回开盘价、当前价格以及价格上涨或下跌的幅度。
我想做的实际上是抓取雅虎在页面上的相关链接-这些链接有与股价相关的文章...例如https://au.finance.yahoo.com/q?s=nok&ql=1向下滚动到标题,我想抓取那些链接。
目前我正在写一本书(用于万维网的 PHP Advanced,我知道它很旧,但我昨天发现它很有趣 :))在书中它说“访问时很重要网页来确切地知道数据在哪里' - 我认为现在有办法解决这个问题......也许能够搜索其中包含特定关键字的链接或类似的东西!
我想知道是否有一个特殊的技巧可以用来抓取网页上的特定数据位?像爬虫一样,它们能够抓取与某物相关的链接。 很高兴知道如何做到这一点,然后我可以在将来将其应用于其他科目。
我将添加我目前拥有的代码。这纯粹是为了练习,因为我在我的课程中学习 PHP :)
##getquote.php
<!DOCTYPE html PUBLIC "-//W3// DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>Get Stock Quotes</title>
<link href='css/style.css' type="text/css" rel="stylesheet">
</head>
<h1>Stock Reader</h1>
<body>
<?php
//Read[1] = current price
//read[5] = opening price
//read[4] = down or up whatever percent from opening according to current price
//Step one
//Begin the PHP section my checking if the form has been submitted
if(isset($_POST['submit'])){
//Step two
//Check if a stock symbol was entered.
if(isset($_POST['symbol'])){
//Define the url to be opened
$url = 'http://quote.yahoo.com/d/quotes.csv?s=' . $_POST['symbol'] . '&f=sl1d1t1c1ohgv&e=.csv';
//Open the url, if can't SHUTDOWN script and write msg
$fp = fopen($url, 'r') or die('Cannot Access YAHOO!.');
//This will get the first 30 characters from the file located in $fp
$read = fgetcsv ($fp, 30);
//Close the file processsing.
fclose($fp);
include("php/displayDetails.php");
}
else{
echo "<div style='color:red'>Please enter a SYMBOL before submitting the form</div>";
}
}
?>
<form action='getquote.php' method='post'>
<p>Symbol: </p><input type='text' name='symbol'>
<br />
<input type="submit" value='Fetch Quote' name="submit">
</form>
<br />
<br />
##displayDetails.php
<div class='display-contents'>
<?php
echo "<div>Todays date: " . $read[2] . "</div>";
//Current price
echo "<div>The current value for " . $_POST["symbol"] . " is <strong>$ " . $read[1] . "</strong></div>";
//Opening Price
echo "<div>The opening value for " . $_POST["symbol"] . " is <strong>$ " . $read[5] . "</strong></div>";
if($read[1] < $read[5])
{
//Down or Up depending on opening.
echo "<div>" .strtoupper($_POST['symbol']) ."<span style='color:red'> <em>IS DOWN</em> </span><strong>$" . $read[4] . "</strong></div>";
}
else{
echo "<div>" . strtoupper($_POST['symbol']) ."<span style='color:green'> <em>IS UP</em> </span><strong>$" . $read[4] . "</strong></div>";
}
将代码添加到 displayDetails.php
function getLinks(){
$siteContent = file_get_contents($url);
$div = explode('class="yfi_headlines">',$siteContent);
// every thing inside is a content you want
$innerContent = explode('<div class="ft">',$div)[0]; //now you have inner content of your div;
$list = explode("<ul>",$innerConent)[1];
$list = explode("</ul>",$list)[0];
echo $list;
}
?>
</div>
我只是输入相同的代码 - 我真的不知道我应该用它做什么?!
【问题讨论】:
-
任何允许您获取其数据的网站都提供 API
标签: php html web-crawler fopen