本地 Google 搜索的搜索结果页面通常类似于 this , 包含 20 个结果。
为了在左侧获得任何给定结果的完整联系方式,需要单击该结果,(在漫长的等待之后)在Google map Pane (在 Firefox 上,在其他网络浏览器上做一些不同的事情):
我正在提取公司名称。地址、电话和网站与 Python 和 WebDriver 因此:
address = driver.find_element_by_xpath("//div[@id='akp_uid_0']/div/div/ol/li/div/div/div/ol/table/tbody/tr[2]/td/li/div/div/span[2]").text
name = driver.find_element_by_css_selector(".kno-ecr-pt").text.encode('raw_unicode_escape')
phone = driver.find_element_by_css_selector("div._mr:nth-child(2) > span:nth-child(2)").text
website = driver.find_element_by_css_selector("a.lua-button:nth-child(1)").get_attribute("href")
这工作可靠,但极度慢。每次加载每个 map 叠加层可能需要数十秒。我已经通过 WebDriver 尝试过 PhantomJS,但很快就被谷歌的机器人检测阻止了。
如果我对 Firebug 的理解是正确的,那么左侧的每个链接都是这样定义的:
<a data-ved="0CA4QyTMwAGoVChMIj66ruJHGxwIVTKweCh03Sgw0" data-async-trigger="" data-height="0" data-cid="11660382088875336582" data-akp-stick="H4sIAAAAAAAAAGOovnz8BQMDgycHm5SIoaGZmYGxhZGBhYWFuamxsZmphZESVtEoyeSMzKL8gqLE5JL8omLtvNRyhcr8omztvMrkA51e-lt5XiW0n3kw-e7MFfkJwUIAxqbXGGYAAAA" data-akp-oq="Body in Balance Chiropractic New York, NY" jsl="$x 3;" data-rtid="ifLMvGmjeYOk" jsaction="r.UQJvbqFUibg" class="ifLMvGmjeYOk-6WH35iSZ2V0 rllt__link rllt__content" tabindex="0" role="link"><div class="_Ml"><div class="_pl _ki"><div role="heading" aria-level="3" style="margin-right:0px" class="_rl">Body in Balance <wbr></wbr>Chiropractic</div><div class="_lg"><span aria-hidden="true" class="rtng" style="margin-right:5px">5.0</span><g-review-stars><span aria-label="Rated 5.0 out of 5" class="_pxg _Jxg"><span style="width:70px"></span></span></g-review-stars><div style="display:inline;font-size:13px;margin-left:5px"><span>20 reviews</span></div></div><div class="_tf"><span>Chiropractor</span> · W 45th St</div><div class="_CRe"><div><span>Opens at 8:00 am</span></div></div></div></div></a>
我对 CSS 和 JavaScript 几乎一无所知,所以我可能没有问对问题。但是有没有一种方法可以获取最终悬停在“ map ” Pane 上的内容的潜在来源(可能有一个更专业的术语),而不必单击左侧的链接来调出它?我的想法是,如果我能够解析 HTML 而无需实际触发它,我可以节省很多时间。
最佳答案
我已尝试检查您提供的页面的 dom 结构。基本上 IE 在这样的页面上与 Firefox 有很大的不同(一旦你点击左侧的元素,IE 将定向到另一个页面。)
但由于我的环境限制,我只能为 IE 完成此操作。对于 firefox,您可以尝试以下代码。可能存在一些小问题(抱歉,我无法测试它)。
注意:我写了一个java demo(只是为了搜索电话号码),因为我熟悉java。而且我也不擅长 cssSelector 所以我改用 xpath。希望对您有所帮助。
driver.get("https://www.google.com/search?q=chiropractors%2Bnew%20york%2Bny&rflfq=1&tbm=lcl&tbs=lf:1,lf_ui:2&oll=40.754671143320074,-73.97722375000001&ospn=0.017814865199625274,0.040340423583984375&oz=15&fll=40.75807315356519,-73.99290368792725&fspn=0.01641614335274255,0.040340423583984375&fz=15&ved=0CJIBENAnahUKEwj1jtnmtcbHAhVTCo4KHfkkCYM&bav=on.2,or.r_cp.&biw=1360&bih=608&dpr=1&sei=y4LdVYvcFsa7uATo_LngCQ&ei=4YTdVbWaENOUuAT5yaSYCA&emsg=NCSR&noj=1&rlfi=hd:;si:#emsg=NCSR&rlfi=hd:;si:&sei=y4LdVYvcFsa7uATo_LngCQ");
//0. Actually no need unless you have low connection speed with google.
Thread.sleep(5000);
//1. By xpath '_gt' will extract all of the 20 results' div on left hand side. Both IE and firefox can work well.
List<WebElement> elements = driver.findElements(By.xpath("//div[@class='_gt']"));
//2. Traverse all of the results. Let 'data-cid' as identifier. Note: Only FF can be done. For IE there are no data-cid s
for(int i=0; i<elements.size(); i++) {
WebElement e = elements.get(i);
WebElement aTag = e.findElement(By.tagName("a"));
String dataCid = aTag.getAttribute("data-cid");
//3. Here, the div which contains the info we want can be identified by 'data-cid' in firefox
WebElement parentDivOfTable = driver.findElement(By.xpath("//div[@class='akp_uid_0' and @data-cid='" + dataCid + "']"));
//4. get the infomation table.
WebElement table = parentDivOfTable.findElement(By.xpath("//table[@class='_B5g']"));
//get the phone num.
String phoneNum = table.findElement(By.xpath("//span[text()='Phone:']/following-sibling")).getText();
}
关于javascript - 如何从谷歌搜索结果 "20-pack"条目中提取源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32220030/