关于selenium中text返回空值的原因
这几天闲来无事,在做爬虫的过程中遇到了一个小问题,获取值的xpath正确,可以正常获取到标签属性。但是获取文本的时候却获取到了空值。
错误代码如下:
from selenium import webdriver
Driver = webdriver.Chrome(executable_path='D:\ALL_WorkSpace\PycharmProject\Selenium_Study\chromedriver.85.0.4183.87')
Driver.get("**网站**")
Informations = Driver.find_elements_by_xpath("//div[@class='s-main-slot s-result-list s-search-results sg-row']/div[@data-uuid]")
for info in Informations:
Price = info.find_element_by_xpath(".//a/span[@class='a-price']/span").text
print(Price+"----")
错误例图:
如果将值存入列表方便观看就类似于—>["","","",""]
解决方法:
将info.find_element_by_xpath(".//a/span[@class='a-price']/span").text
改成info.find_element_by_xpath(".//span[@class='a-offscreen']").get_attribute('textContent')
就可以正常获取到标签里的文本了。
原因:.text的适用范围的问题
requests对象的get和post方法都会返回一个Response对象,这个对象里面存的是服务器返回的所有信息,包括响应头,响应状态码等。其中返回的网页部分会存在.content和.text两个对象中。
两者区别在于,content中间存的是字节码,而text中存的是Beautifulsoup根据猜测的编码方式将content内容编码成字符串。
所以简而言之,.text是现成的字符串,.content还要编码,但是.text不是所有时候显示都正常,这是就需要用.content进行手动编码。
更详细的解释参考:https://zhidao.baidu.com/question/941417472703558372.html