爬虫,爬取中国工程院院士信息

编程入门 行业动态 更新时间:2024-10-18 16:45:48

<a href=https://www.elefans.com/category/jswz/34/1770264.html style=爬虫,爬取中国工程院院士信息"/>

爬虫,爬取中国工程院院士信息

爬虫,爬取中国工程院院士信息:

import os
import re
from urllib.request import urlopendirname='yuanshi'
if not os.path.isdir(dirname):os.mkdir(dirname)
os.chdir(dirname)
dirname=os.getcwd()url=r'.html'
with urlopen(url) as fp:content=fp.read().decode()pattern=r'<li class=".*?"><a href="(.+?)" target=".*?">(.+?)</a></li>'content=re.findall(pattern,content)for url,name in content:os.chdir(dirname)print("正在载入%s的信息,waiting....."%name)if not os.path.isdir(name):os.mkdir(name)os.chdir(name)url=r''+urlwith urlopen(url) as fp:txt=fp.read().decode()pattern=r'<img src="(.+?)" style=".*?"/>'pic=re.findall(pattern,txt)pic=pic[0].replace(' ',r'%20')pic=r''+picwith open(name+'.jpg','wb') as fp:fp.write(urlopen(pic).read())pattern=r'<p>(.+?)</p>'info=re.findall(pattern,txt)info='\n'.join(info)pattern=r'<a.+?</a>|(&ensp;)|(&nbsp;)'info=re.sub(pattern,'',info)with open(name+'.txt','w',encoding='utf8') as fp:fp.write(info)print('信息读取完毕')

更多推荐

爬虫,爬取中国工程院院士信息

本文发布于:2024-02-12 05:28:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1686307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爬虫   中国   工程院院士   信息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!