python 爬取网页出错 菜鸟求解
#encoding=UTF-8fromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfro...
#encoding=UTF-8from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom bs4 import BeautifulSoupimport reimport timedriver = webdriver.Chrome()wait = WebDriverWait(driver,10)driver.get('http://ptpress.com.cn/search/books')search_btn = driver.find_element_by_id("searchVal")search_btn.send_keys('python编程')#input = wait.until(EC.presence_of_element_located(By.ID,'searchVal'))confirm_btn = wait.util( EC.element_to_be_clickable(By.CSS_SELECTOR,'#app> div:nth-child(1) > div > div > div > button >i'))confirm_btn.click()time.sleep(5)html = driver.page_sourcesoup = BeautifulSoup(html,'lxml')a = soup.select('.rows')lsl = '<img src="(.*?)"/><div>'pattern = re.compile(lsl,re.S)ls2='<img src=".*?"/></div>.*?<p>(.*?)</p></ a>'pattern1 = re.compile(ls2,re.S)res_test = re.findall(pattern,str(a))print(res_test,res_img)driver.close()
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询