【发布时间】:2014-03-28 12:52:40
【问题描述】:
您可能已经在这里看到了过去几周我绝望的挫败感。我一直在抓取一些等待时间数据,但仍然无法从这两个站点获取数据
起初我尝试了 BS4 for Python。下面是 HCA Virgina 的示例代码
from BeautifulSoup import BeautifulSoup
import requests
url = 'http://hcavirginia.com/home/'
r = requests.get(url)
soup = BeautifulSoup(r.text)
wait_times = [span.text for span in soup.findAll('span', attrs={'class': 'ehc-er-digits'})]
fd = open('HCA_Virginia.csv', 'a')
for w in wait_times:
fd.write(w + '\n')
fd.close()
所有这些都是在控制台或 CSV 中打印空白。所以我用 PhantomJS 试了一下,因为有人告诉我它可能是用 JS 加载的。然而,同样的结果!将空白打印到控制台或 CSV。下面的示例代码。
var page = require('webpage').create(),
url = 'http://hcavirginia.com/home/';
page.open(url, function(status) {
if (status !== "success") {
console.log("Can't access network");
} else {
var result = page.evaluate(function() {
var list = document.querySelectorAll('span.ehc-er-digits'), time = [], i;
for (i = 0; i < list.length; i++) {
time.push(list[i].innerText);
}
return time;
});
console.log (result.join('\n'));
var fs = require('fs');
try
{
fs.write("HCA_Virginia.csv", '\n' + result.join('\n'), 'a');
}
catch(e)
{
console.log(e);
}
}
phantom.exit();
});
Centura Health 存在同样的问题 :(
我做错了什么?
【问题讨论】:
-
试试ghost.py - 它应该为你加载所有的JS。虽然速度可能有点慢,但我会建议你检查一下。 (免责声明:我还没有检查你的代码)
-
似乎
ghost.py有很多错误 - 上次它对我有用,但现在我的测试脚本因错误退出......:/ -
我认为这可能是页面的某种负载问题?我已经在许多其他网站上成功使用了 BS4 和 Phantom。这些网站把它扔掉......
标签: javascript python web-scraping beautifulsoup phantomjs