如何将python爬出来的链接保存到Excel中仍然能点击
openpyxl 有hyperlink属性
from openpyxl import Workbook
wb=Workbook()
ws=wb.active
href="https://ask.csdn.net/questions/7603171"
ws.cell(row=1,column=1,value=href)
ws.cell(row=1,column=1).hyperlink=href///////////
有帮助或启发麻烦点下【采纳该答案】
将获取的数据写成dataframe,用循环添加行数据并在写入链接时指定一下属性为hyperlink即可,参考代码:
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
import requests
from bs4 import BeautifulSoup as bs
import pandas as pd
#用requests.get获取数据,bs4的selecet选择解析出数据(列表形式)
df=pd.DataFrame({'titles':titles,'links':links})
wb=Workbook()
ws=wb.active
for i,r in enumerate(dataframe_to_rows(df, index=True, header=True)):
ws.append(r)
if i>2:
ws.cell(i,3).hyperlink=r[2]
wb.save('abcd.xlsx')
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!