qq_40719612
qq_40719612
2019-03-13 17:52

selenium爬取淘宝美食数据的问题

  • python
  • chrome

我的代码是这样的

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
def search():
    try:
        browser.get('https://www.taobao.com')
        input = wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR, '#q'))
        )
        submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#J_TSearchForm > div.search-button > button')))
        input.send_keys('美食')
        submit.click()
        total = wait.until(
            EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#mainsrp-pager > div > div > div > div.total'))
        )
        return total.text
    except TimeoutException:
        return search()

def main():
    total = search()
    print(total)


if __name__ == '__main__':
    main()

到这一步时我希望看一下输出结果,可是出现了报错

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/tbms/spider.py", line 29, in <module>
    main()
  File "C:/Users/Administrator/PycharmProjects/tbms/spider.py", line 25, in main
    total = search()
  File "C:/Users/Administrator/PycharmProjects/tbms/spider.py", line 20, in search
    return total.text
AttributeError: 'list' object has no attribute 'text'

Process finished with exit code 1

看到站内其它的淘宝美食数据都是这样写的,有些不懂问题出在哪里

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐

换一换