[英]how to web scrape a google results?
我需要獲取谷歌結果頁面的內容,如下所示:
我嘗試使用此代碼使用 xpath,但沒有找到元素
import lxml.html
import requests
html= requests.get("https://www.google.com/search?q=curitiba")
lxml = lxml.html.fromstring(html.content)
test=lxml.xpath('/html/body/div[7]/div[2]/div[9]/div[3]/div/div/div[1]/div[2]/div/div/div/div[1]/div/div/div/div[1]/div/div/div/div/span/text()')
print(test)
這是chrome本身提供的xpath
我怎樣才能得到這個頁面的內容?
使用BeautifulSoup
:
import bs4
import requests
html = requests.get("https://www.google.com/search?q=curitiba")
soup = bs4.BeautifulSoup(html.content)
targeth3 = soup.find("h3", string="Descrição") # Finds the h3 tag above the span
targetspantext = targeth3.nextSibling.text # access the text in the target span tag
編輯:您無法通過請求檢索該框,因為它已加載 javascript。 您可以使用 selenium 或使用https://serpapi.com/ 。 您可以使用 API 檢索該框,它被稱為“知識圖”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.