import openpyxl
import requests
from bs4 import BeautifulSoup
打开excel
wb = openpyxl.load_workbook('test.xlsx')
表单 Sheet1
sh = wb['Sheet1']
循环处理每行第一列
for col in list(sh.columns)[0]:
# 获取内容
html = requests.get(col.value)
bs = BeautifulSoup(html.text)
# 获取id为list的div,我看了网页,指向每章的标签a都在该div下
div = bs.find(id='list')
# id为list里所有a标签
a_list = div.find_all('a')
# 从该行第二列开始
for i in range(2,len(a_list)+2):
# 写入
sh.cell(col.row,i,a_list[i-2].text)
print(col.value + '好了')
保存
wb.save('test.xlsx')
关闭
wb.close()