600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > [Python]提取docx/网页超链接

[Python]提取docx/网页超链接

时间:2022-12-13 04:05:11

相关推荐

[Python]提取docx/网页超链接

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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。