[英]How to use IMPORTXML to get URL of first result of Google Search?
I am trying to use the ImportXML function in Google Spreadsheets to find the URL of the first result of a Google Search (not including any links to advertised links). 我正在尝试使用Google Spreadsheets中的ImportXML函数来查找Google搜索的第一个结果的网址(不包括指向广告链接的任何链接)。 I thought I understood that all search results are displayed in an h3 element with class "r", and I hoped the XPath expression below would find a) all links to search results (the part in brackets) and then b) select the first one of those:
我以为我理解所有搜索结果都显示在h3元素中,类为“ r”,我希望下面的XPath表达式可以找到a)搜索结果的所有链接(括号中的部分),然后b)选择第一个那些:
=importxml(A1,"(//h3[@class='r']/a/@href)[1]")
[where A1 is eg "="https://www.google.com/search?q=U+Chicago&num=10""] [其中A1例如“ =“ =” https://www.google.com/search?q=U+Chicago&num=10“”]
Unfortunately I do get links to Google Maps. 不幸的是,我确实获得了指向Google地图的链接。
Instead of filtering through the results, you should set google parameters to only return one result. 而不是过滤结果,您应该将google参数设置为仅返回一个结果。 The query parameters should include the number(num) of responses and the page (start)
查询参数应包括响应数(num)和页面(开始)
"https://www.google.com/search?q=stack+overflow&num=1&start=1"
按相关ID缩小查询范围,例如:
//ol[@id='rso']//h3[@class='r']/a/@href
I've been struggling with the same thing. 我一直在努力做同样的事情。 Haven't found a way to get a unique XPath query that only returns one URL, but you can force Google to return only the top search result!
还没有找到一种方法来获取仅返回一个URL的唯一XPath查询,但是您可以强制Google仅返回顶部搜索结果!
In your search URL, change "&num=10" to "&num=1". 在您的搜索URL中,将“&num = 10”更改为“&num = 1”。 That's it.
而已。
您可以同时执行两种操作,也可以使用“&num = 1”返回一个结果,也可以使用xpath“ // cite”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.