import requests
from lxml import etree
import pymysql
# 获取网页源代码
res = requests.get('https://beijing.8684.cn/x_35b1e697')
# 解析
tree = etree.HTML(res.text)
station = tree.xpath('//ol[@class="ol-wrap"]/li/a/text()') #所有的站点名称
href = tree.xpath('//ol[@class="ol-wrap"]/li/a/@href') #所有属性值
#存储
# 1、连接数据库
connect = pymysql.Connect(host='localhost', port=3306, user='root', passwd='123456', database='bus')
print('连接成功')
# 2、创建游标
cursor = connect.cursor()
# 3、使用游标执行sql语句
for i in range(len(station)):
# print(station[i],href[i])
sql = f"insert into bus_328(station, href) values(%s,%s);"
cursor.execute(sql,(station[i],href[i]))
# 4、提交
connect.commit()
# 5、执行成功
print('执行成功')
cursor.close()
connect.close()
selenium谷歌浏览器驱动下载地址Selenium安装WebDriver最新Chrome驱动(含116/117/118/119)_chromedriver-CSDN博客