我一直在尝试使用 Selenium WebDriver (Chrome) 加载页面并获取 disqus 评论线程。请查看代码。
public static void Main(string[] args)
{
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://nation.com.pk/blogs/05-Apr-2016/should-qandeel-baloch-s-strip-tease-really-be-a-rallying-cause-for-liberalism");
var userNameField = driver.FindElementById("disqus_thread").GetAttribute("outerHTML");
Console.WriteLine(userNameField);
Console.Read();
}
它给出以下输出。
<div id="disqus_thread"><iframe id="dsq-app2" name="dsq-app2" allowtransparency="true" frameborder="0" scrolling="no" tabindex="0" title="Disqus" width="100%" src="http://disqus.com/embed/comments/?base=default&version=af1a2e2611136ef6c314afec2806cef3&f=nawaiwaqt&t_u=http%3A%2F%2Fnation.com.pk%2Fblogs%2F05-Apr-2016%2Fshould-qandeel-baloch-s-strip-tease-really-be-a-rallying-cause-for-liberalism&t_d=Should%20Qandeel%20Baloch%E2%80%99s%20%E2%80%98striptease%E2%80%99%20really%20be%20a%20rallying%20cause%20for%20liberalism%3F&t_t=Should%20Qandeel%20Baloch%E2%80%99s%20%E2%80%98striptease%E2%80%99%20really%20be%20a%20rallying%20cause%20for%20liberalism%3F&s_o=default" style="width: 1px !important; min-width: 100% !important; border: none !important; overflow: hidden !important; height: 652px !important;" horizontalscrolling="no" verticalscrolling="no"></iframe></div>
这与我的期望相反,因为我希望在这个阶段获得评论 html。如何修改此代码以获得评论线程? PS:原始页面源码中没有iframe的指示。
最佳答案
这就是我如何获取 iframe 源代码以进行进一步处理。希望它对其他人有帮助。
var driver = new ChromeDriver();
int tmp = 1;
foreach(string file in File.ReadLines(@"bla bla bla\Bloglinks.txt"))
{
driver.Navigate().GoToUrl(file);
driver.SwitchTo().Frame("dsq-app2");
var userNameField = driver.PageSource;
File.WriteAllText(@"bla bla bla\Comments\"+tmp+".html", userNameField);
Console.WriteLine(file);
tmp++;
}
driver.Close();
关于javascript - 如何使用 Selenium WebDriver 获取 disqus 评论 iframe 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36810584/