简体   繁体   English

如何使用IMPORTXML获取Google搜索的第一个结果的URL?

[英]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.

相关问题 google docs中的importxml并返回google搜索结果 - importxml in google docs and returning a google search result ImportXML for Google Result表示:“ Google表格”中的“无法获取URL” - ImportXML for Google Result says: “Could not fetch URL” in Google Sheet IMPORTXML Google 资源位于 url - IMPORTXML Google resource at url 如何在ImportXML的URL中包含Google表格单元格引用 - How to include a Google Sheets cell reference in URL for ImportXML 如何在Google文档中使用ImportXML XPath concat? - How to use ImportXML XPath concat within Google Docs? 谷歌表:IMPORTXML():我如何获得“日期时间”? - Google sheets: IMPORTXML(): How do I get 'datetime'? 我们如何在 Google 表格中使用 =IMPORTXML function 从自定义属性或使用 Angular 生成的属性中获取数据? - How can we use the =IMPORTXML function in Google Sheets to get the data from a custom attribute or an attribute generated using Angular? 我试图在谷歌表格中使用 importxml 从谷歌金融获取股票价格,在 xml 元素和语法中苦苦挣扎 - Im trying to use importxml in google sheets to get a stock price from google finance, struggling with xml elements and syntax ImportXML获取内容无法使用Google表格进行解析 - ImportXML get content cannot be parsed with Google Sheets Xpath:如何使用importXML函数从Google电子表格的URL中提取值? - Xpath: How to extract values from URL in Google spreadsheet using importXML function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM