from pydocx import PyDocXfrom bs4 import BeautifulSoup # 用于解析网页# 转docx为html文本html = PyDocX.to_html("docx文本名")# 加载文本bsObj = BeautifulSoup(html, 'html.parser')# 提取所有<a>方法t1 = bsObj.find_all('a')txt = ''#统计结果写入txt文档with open('计.txt', 'w', encoding='utf8') as fn:for i in range(len(t1)):#提取超链接t2 = (t1[i]).get('href')#提取链接文本t3 = (BeautifulSoup(str(t1[i]), 'html.parser')).a.stringsfn.write(''.join(t3)+':'+str(t2)+'\n')#关闭文档fn.close()
网页提取超链接并写入TXT文档:
from urllib.request import urlopen#用于获取网页from bs4 import BeautifulSoup#用于解析网页#by: 菜鸟阿洋#在此输入网址html = urlopen('网址链接')bsObj = BeautifulSoup(html, 'html.parser')# 提取所有<a>标签t1 = bsObj.find_all('a')txt = ''#统计结果写入txt文档with open('html统计结果.txt', 'w', encoding='utf8') as fn:for i in range(len(t1)):#提取超链接t2 = (t1[i]).get('href')#提取链接文本t3 = (BeautifulSoup(str(t1[i]), 'html.parser')).a.stringsfn.write(''.join(list(t3))+':'+str(t2)+'\n')#关闭文档fn.close()