经过3个小时的努力尝试,我自己决定自己不可能或不可能做。我的问题如下:
如何使用PHP在网页中回显它们,以刮除所附图像中的数字?
图片网址:http://gyazo.com/6ee1784a87dcdfb8cdf37e753d82411c
请帮忙。我已经尝试了几乎所有东西,从使用cURL到使用正则表达式,再到尝试xPath。没有一件事情做对了。
我只希望数字本身能够被隔离,分配给变量,然后在页面其他位置回显。
更新:
http://youtube.com/exonianetwork-我要抓取的URL。
/html/body[@class='date-20121213 en_US ltr ytg-old-clearfix guide-feed-v2 site-left-aligned exp-new-site-width exp-watch7-comment-ui webkit webkit-537']/div[@id='body-container']/div[@id='page-container']/div[@id='page']/div[@id='content']/div[@id='branded-page-default-bg']/div[@id='branded-page-body-container']/div[@id='branded-page-body']/div[@class='channel-tab-content channel-layout-two-column selected blogger-template ']/div[@class='tab-content-body']/div[@class='secondary-pane']/div[@class='user-profile channel-module yt-uix-c3-module-container ']/div[@class='module-view profile-view-module']/ul[@class='section'][1]/li[@class='user-profile-item '][1]/span[@class='value']
我尝试过的xPath,由于某种未知的原因而无法使用。没有引发异常或错误,并且什么也没有显示。
最佳答案
也许简单的XPath会更易于操作和调试。
这是Short Self-Contained Correct Example(注意class
名称末尾的空格):
#!/usr/bin/env php
<?
$url = "http://youtube.com/exonianetwork";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
if (!$html)
{
print "Failed to fetch page. Error handling goes here";
}
curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$profile_items = $xpath->query("//li[@class='user-profile-item ']/span[@class='value']");
if ($profile_items->length === 0) {
print "No values found\n";
} else {
foreach ($profile_items as $profile_item) {
printf("%s\n", $profile_item->textContent);
}
}
?>
执行:
% ./scrape.php
57
3,593
10,659,716
113,900
United Kingdom
关于php - 使用PHP从YouTube抓取统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13871642/