如果'tags'是正确的术语,我不是,但我必须将此跨度中的“数据时间”值转换为数组。我怎样才能使用简单的html dom来获取它们?
这是关于我想要获取“数据时间”的范围。
include('../simpleHtmlDom/simple_html_dom.php');
// Put the Twitters username here
$user = "yadayada";
$html = file_get_html("https://twitter.com/$user");
$ret = $html->find('div[class=ProfileTweet-contents]');
$ret = $html->find('p[class=ProfileTweet-text js-tweet-text u-dir]');
/// tries to get the time code but does only gets the span
$date = $html->find('span[class=js-short-timestamp js-relative-timestamp]', 0);
$DoesNotWork = $html->find( "data-time", 0 );
echo $ret[1]; // get's a users tweet.
echo $DoesNotWork;
日期的结果
<span class="js-short-timestamp js-relative-timestamp"
data-time="1401528672"
data-long-form="true">
15h
</span>
我认为它是这样的,但这段代码不起作用。 $ html-&gt; find(“data-time”,0);
答案 0 :(得分:2)
你可以试试这个:
// Include the script
$url = 'https://twitter.com/yourusername';
$html = file_get_html($url);
$dateTimes = array();
foreach ($html->find('div.GridTimeline .js-short-timestamp') as $value) {
$dateTimes[] = $value->innertext;
}
print_r($dateTimes);
的结果:
Array
(
[0] => 2h
[1] => 2h
[2] => 2h
// Truncated...
[10] => 11h
[11] => May 30
[12] => May 30
[13] => May 6
// Truncated...
)
答案 1 :(得分:0)
我能够使用此代码获取日期,因此我认为有更好的方法。我认为最好找一个简单的dom代码来获取日期时间的文本
<span class"js-short-timestamp js-relative-timestamp" date-time="89393748474">
但我使用了两个&#34; list&#34; php行如下图所示,有效。
$dateTimes = array();
foreach ($html->find('div.GridTimeline .js-short-timestamp') as $value) {
$dateTimes[] = $value->outertext;
}
// These are the lines I get the date-time from.
list($Gone,$Keep) = explode("data-time=\"", $dateTimes[0]);
list($Date,$Gone) = explode("\"", $Keep);
$Date = date('M d, Y', $Date);
答案 2 :(得分:0)
您想使用:
$html->find( "[data-time]", 0 );
答案 3 :(得分:0)
万一有人在 2021 年登陆这里,遵循第 1 个谷歌搜索结果:
除非我误解了您的意图,否则您可能会使用(使用 simplehtmldom)实现您想要的:
$html->find('span[data-time]')->attr[data-time];
官方 simplehtmldom 文档没有提到这一点。但是,https://stackoverflow.com/a/14456823/10050838 是一种可能的来源。