我正在尝试从Google搜索结果页中的以下查询https://www.google.com/search?q=site:https://theshipibomarket.com/中获取元素id='resultStats'
中的文本
我得到的代码已经输出,但不是元素中的文本。输出为:
[<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="5a1e4063-dcb8-48b2-93f6-1c60bb7e9e05", element="63dabd48-bd5f-4380-9598-173b91e72367")>]
当我在.text
元素上使用results
函数时,出现以下错误:
AttributeError: 'list' object has no attribute 'text'
这是我的代码:
# import libraries
import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
options = Options()
options.headless = True
query = "site:https://theshipibomarket.com/"
urlpage = "https://www.google.com/search?q="+query
print(urlpage)
# run firefox webdriver from executable path of your choice
driver = webdriver.Firefox(options=options)
# get web page
driver.get(urlpage)
# execute script to scroll down the page
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
# sleep for 30s
time.sleep(30)
# driver.quit()
# find elements by xpath
results = driver.find_elements_by_xpath("//*[@id='resultStats']")
#print('Number of results', len(results))
print("The number of pages Google have index {}".format(results.text))
我怀疑这是由javascript引起的,因为输出为list
。我没有刮刮Google的经验,也没有做很多刮刮操作,因此,如果这代表我是一个简单的误会,请您道歉。
答案 0 :(得分:1)
将最后一行更改为:
print("The number of pages Google have index {}".format(results[0].text)) # added zero