用python的lxml、pandas、selenium编写一段代码。 以https://www.sciencedirect.com/journal/the-lancet为初始界面,等待10秒,用selenium点击该页面上的链接(class="anchor js-volume volume-issue-text anchor-default"),跳转后等待10秒,用selenium点击该页面上的链接(class="switch-check switch-small js-previews-switch"),等待10秒,获取该页面上所有文章的标题、摘要、作者。建立excel表格,将结果导入excel表格
时间: 2023-03-19 16:22:39 浏览: 89
以下是用python的lxml、pandas、selenium编写的一段代码:from selenium import webdriver
from lxml import etree
from pandas import DataFrame
import time# 初始化浏览器
browser = webdriver.Chrome()
# 打开初始界面
browser.get('https://www.sciencedirect.com/journal/the-lancet')
time.sleep(10)
# 用selenium点击链接
link1 = browser.find_element_by_css_selector('.anchor.js-volume.volume-issue-text.anchor-default')
link1.click()
time.sleep(10)
# 用selenium点击链接
link2 = browser.find_element_by_css_selector('.switch-check.switch-small.js-previews-switch')
link2.click()
time.sleep(10)
# 获取网页源代码
html = browser.page_source
# 关闭浏览器
browser.close()
# 使用lxml解析网页源代码
selector = etree.HTML(html)
# 获取文章标题
title = selector.xpath('//h2[@class="ArticleTitle"]/text()')
# 获取摘要
summary = selector.xpath('//p[@class="Para"]/text()')
# 获取作者
authors = selector.xpath('//span[@class="text-s"]/text()')
# 将结果存入excel表格
dataframe = DataFrame({'文章标题':title, '摘要':summary, '作者':authors})
dataframe.to_excel('the-lancet.xls')