Python爬虫之selenium爬取豆瓣Python分类书籍

非常简单,找到网页,获取网页内容,xpath查找,输出

https://book.douban.com/subject_search?search_text=python&cat=1001&start=%s0

from selenium import webdriver
import time
from lxml import etree

#获取网页内容
def get_web(url):
    driver = webdriver.Chrome()
    driver.get(url)
    #强制等待网页加载
    time.sleep(10)
    driver.save_screenshot('douban.png')
	#保存网页文件
    fn = 'douban.html'
    with open(fn,'w',encoding='utf-8') as f:
        f.write(driver.page_source)

    content_parse(fn)
    #退出浏览器
    driver.quit()
    
#解析网页
def content_parse(fn):
	#读取网页内容
    with open(fn,'r',encoding='utf-8')as f:
        html = f.read()
        print(type(html))
    tree = etree.HTML(html)
	#通过书籍标签查找书名
    books = tree.xpath("//div[@class='item-root']")
    for book in books:
        book_name = book.xpath(".//div[@class='title']/a")
        print(book_name[0].text)

if __name__ == '__main__':
    url ='https://book.douban.com/subject_search?search_text=python&cat=1001&start=%s0'
    get_web(url)

你可能感兴趣的:(selenium)