python爬取页面内容

from selenium import webdriver
import xlwt

driver = webdriver.Chrome(r'D:\chromedriver.exe')
driver.maximize_window() #窗口最大化
workbook = xlwt.Workbook(encoding='ascii')
worksheet = workbook.add_sheet('抓取内容')
a1="https://baike.baidu.com/item/%E5%8D%8E%E4%B8%BAMate30/22592869?fr=aladdin"
a2="https://baike.baidu.com/item/%E5%8D%8E%E4%B8%BAP30/22778077?fr=aladdin"
a3="https://baike.baidu.com/item/%E5%8D%8E%E4%B8%BAP20/21971588?fr=aladdin"
for i in range(4):
if i == 1:
z = a1
driver.get(z)
v = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/div[7]/dl[1]/dd[1]')
r0 = v.text
v1 = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/div[7]/dl[1]/dd[2]')
r1 = v1.text
v2 = driver.find_element_by_xpath('/html/body/div[4]/div[2]/div/div[2]/div[7]/dl[1]/dd[3]')
r2 = v2.text
worksheet.write(i, 0, r0) # 写入
worksheet.write(i, 1, r1) # 写入
worksheet.write(i, 2, r2) # 写入

上一篇:【转】C# 中 10 个你真的应该学习(和使用!)的功能


下一篇:Python 爬虫入门(一)——爬取糗百