python提取xml里面的鏈接源碼詳解
因群里朋友需要提取xml地圖里面的鏈接,就寫了這個(gè)程序。
代碼:
#coding=utf-8 import urllib import urllib.request import re url='http://zhimo.yuanzhumuban.cc/sitemaps.xml' html=urllib.request.urlopen(url).read() html=html.decode('utf-8') r=re.compile(r'(http://zhimo.yuanzhumuban.cc.*?\.html)') big=re.findall(r,html) for i in big: print(i) op_xml_txt=open('xml.txt','a') op_xml_txt.write('%s\n'%i)
擴(kuò)展閱讀:
Python3提取xml文件中的內(nèi)容
import xml.dom.minidom def find_child(Par_nodes, mystr): for child_node in Par_nodes: if(len(child_node.childNodes) > 0): mystr = find_child(child_node.childNodes, mystr) elif(child_node.nodeValue != None): mystr += child_node.data.replace('\n', '') return mystr if __name__ == '__main__': dom1 = xml.dom.minidom.parse('2.XML') #打開xml文件 root = dom1.documentElement #得到文檔元素對(duì)象 app_nums = root.getElementsByTagName('base:DocNumber') #按標(biāo)簽名稱查找,返回標(biāo)簽結(jié)點(diǎn)數(shù)組 app_num = app_nums[2] print('專利申請(qǐng)?zhí)枺?+app_num.firstChild.data) titles = root.getElementsByTagName('business:InventionTitle') title = titles[0] print('專利名稱:'+title.firstChild.data) Paragraphs = root.getElementsByTagName('base:Paragraphs') abstract = Paragraphs[0] print('專利摘要:'+abstract.firstChild.data) company_names = root.getElementsByTagName('base:Name') company_name = company_names[0] print('公司名稱:'+company_name.firstChild.data) mystr = '' for i in range(len(Paragraphs)): if (Paragraphs[i].firstChild.data == '發(fā)明內(nèi)容\n\t'): i+=1 while Paragraphs[i].firstChild.data != '附圖說明\n\t': mystr = find_child(Paragraphs[i].childNodes, mystr) i+=1 print('發(fā)明內(nèi)容:' + mystr)
以上就是本次介紹的全部實(shí)例代碼知識(shí)點(diǎn),感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。
相關(guān)文章
Python解析命令行讀取參數(shù)--argparse模塊使用方法
這篇文章主要介紹了Python解析命令行讀取參數(shù)--argparse模塊使用方法,需要的朋友可以參考下2018-01-01機(jī)器學(xué)習(xí)實(shí)戰(zhàn)之knn算法pandas
這篇文章主要為大家詳細(xì)介紹了機(jī)器學(xué)習(xí)實(shí)戰(zhàn)之knn算法pandas,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實(shí)例
今天小編就為大家分享一篇tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python @property及getter setter原理詳解
這篇文章主要介紹了Python @property及getter setter原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Django csrf 兩種方法設(shè)置form的實(shí)例
今天小編就為大家分享一篇Django csrf 兩種方法設(shè)置form的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02使用python找出list列表中相同元素(指定元素)的所有索引
這篇文章主要給大家介紹了關(guān)于使用python找出list列表中相同元素(指定元素)的所有索引,在平時(shí)開發(fā)過程中經(jīng)常遇到需要在數(shù)據(jù)中獲取特定的元素索引的信息,需要的朋友可以參考下2023-08-08