搜索Python编程获取相关图书信息
1.获取相关图书信息
#搜索“Python编程”获取相关图书信息
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://www.ptpress.com.cn/shopping/index')
wait = WebDriverWait(driver, 10) # 等待10秒
In [ ]:
before = driver.current_window_handle # 获取标记窗口
In [ ]:
search_btn = driver.find_element_by_css_selector( #定位搜素框
'#header > div.container > div > div.col-md-8.tools > div.search > input[type=text]'
)
search_btn.send_keys('python编程') #在搜索框输入
In [ ]:
confirm_btn = wait.until(
EC.element_to_be_clickable( # 元素可以被点击
(By.CSS_SELECTOR, '#header > div.container > div > div.col-md-8.tools > div.search > button')
)
)
confirm_btn.click() # 点击此按钮 会弹出新的界面
In [ ]:
driver.switch_to_window(driver.window_handles[1]) # 移动到第二个窗口(才能获取第二个窗口的网址内容
html = driver.page_source
In [ ]:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
dat = [i.text for i in soup.select('#books_content > div > div > a > p')]
print(dat)
In [ ]:
import pandas as pd
pd.DataFrame({'价格':dat})
2.参考文章
【创作不易,望点赞收藏,若有疑问,请留言,谢谢】