不知道为什么就只爬了第一页的数据,我也给了休息的时间呀,求大佬们看一下
from selenium import webdriver
from lxml import etree
from time import sleep
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get('https://www.ptpress.com.cn/search/books')
driver.find_element(By.ID,'searchVal').send_keys("python")
driver.find_element(By.XPATH,'//*[@id="app"]/div[1]/div/div/div[2]/button').click()
sleep(3)
tree=etree.HTML(driver.page_source)
list=tree.xpath('//*[@id="books_content"]')
for t in range(0,38):
sleep(5)
f=open('测试.txt', "a+", encoding='utf-8')
for tu in list:
for i in range(0,9):
title= "".join(tu.xpath('.//div/a/p/text()').split())
href="https://www.ptpress.com.cn"+ tu.xpath('.//div/div/a/@href')
print(title)
f.write("标题:"+title+" url: "+href+"\n")
driver.find_element(By.CLASS_NAME,"ivu-page-next").click()
f.close()
driver.close()
driver.quit()