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

我的代码是这样的

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个回答

你这个有两个问题,一个是淘宝是要登录的,你进不去下个网页,第二个问题是你的报错,你可以print(total)看下,total里面是什么,你要的是什么,我看了一下淘宝的网页结构,估计应该是淘宝标题的几个div和美食没啥关系

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐