Python使用BeautifulSoup进行爬虫
2017-07-21 09:29
399 查看
import urllib.request import re from bs4 import BeautifulSoup def getHtml(url): page = urllib.request.urlopen(url) html = page.read() html = html.decode('utf-8') return html def getyema(url): reg = r'<a href="(.*?)">\d</a>' imgre = re.compile(reg) imglist = imgre.findall(url) for imgurl in imglist: if(imgurl.endswith('html')): print(imgurl) def getgaishu(html): soup = BeautifulSoup(html, "html.parser") divPager = soup.find_all('div', class_='detailc') for imgurl in divPager: print(imgurl.get_text()) def getXiangguanjibing(html): reg = r'<dt><a href=".*?" target="_blank" title=".*?">(.*?)</a></dt>' imgre = re.compile(reg) imglist = imgre.findall(html) zhPattern = re.compile(u'[\u4e00-\u9fa5]+') for imgurl in imglist: match = zhPattern.search(imgurl) if match: print(imgurl) def getbansuizhengzhuang(html): # reg = r'<div class=".*?"><ul class=".*?"><p><span>.*?</span>.*?</p><a href=".*?" target="_blank" class=".*?">.*?</a></ul><ul class=".*?">(.*?)</ul></div>' # reg = r'<ul class=".*?"><p><span>.*?</span>(.*?)</p>' reg = r'<li><a href="/symptom/detail/.*?" target="_blank" title=".*?">(.*?)</a></li>' imgre = re.compile(reg) imglist = imgre.findall(html) for imgurl in imglist: print(imgurl) def getallgaishu(html): reg = r'<li ><a href="(.*?)">(.*?)</a></li>' imgre = re.compile(reg) imglist = imgre.findall(html) for imgurl in imglist: print(imgurl) def getalltxt(html): reg = r'<meta name="description" content="(.*?)"/>' imgre = re.compile(reg) imglist = imgre.findall(html) for imgurl in imglist: print(imgurl) def getzhengzhuangqiyin(html): reg = r'<div id="art_content" .*?>(.*?)</div>' imgre = re.compile(reg) imglist = imgre.findall(html) for imgurl in imglist: print(imgurl) def GetBlogPage(html): # str(html, 'utf-8') soup = BeautifulSoup(html, "html.parser") divPager = soup.find_all('div', id='art_content') for trtag in divPager: tdlist = trtag.find_all('p') for trt in tdlist: td = trt.find_all('p') for t in td: print(t.get_text()) def onlygetbansuizhengzhuang(html,zonghtml): # reg = r'<div class=".*?"><ul class=".*?"><p><span>.*?</span>.*?</p><a href=".*?" target="_blank" class=".*?">.*?</a></ul><ul class=".*?">(.*?)</ul></div>' # reg = r'<ul class=".*?"><p><span>.*?</span>(.*?)</p>' soup = BeautifulSoup(html, "html.parser") divPager = soup.find_all('ul', class_='catalog02 z_catalog02e') i = 0; # 获取更多ul class="z_border01_head" div class="z_block08_con" gengduoPager=soup.find_all('div',class_='z_border01') for trtag in divPager: i = i + 1 if(i == 2): for url in trtag.parent.find_all('ul',class_='z_border01_head'): for a in url.find_all('a'): print(a) print(a.get('href')) html=zonghtml+a.get('href') imglist = trtag.find_all('a') for imgurl in imglist: imgurl.get_text() html = 'http://zzk.fh21.com.cn/symptom/detail/1.html' html = getHtml(html) onlygetbansuizhengzhuang(html)
相关文章推荐
- Python 爬虫(以赛马数据为例)之使用BeautifulSoup进行Html解析
- 使用python进行爬虫学习(一)
- 讲解Python的Scrapy爬虫框架使用代理进行采集的方法
- Python爬虫包 BeautifulSoup 学习(十) 各种html解析器的比较及使用
- python3实现网络爬虫(4)--BeautifulSoup使用(3)
- Python爬虫urllib笔记(四)之使用BeautifulSoup爬取百度贴吧
- Python爬虫之使用BeautifulSoup解析HTML文本
- Python3.7 爬虫(三)使用 Urllib2 与 BeautifulSoup4 爬取网易云音乐歌单
- 使用python进行爬虫下载网易云音乐
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。
- python学习(6):python爬虫之requests和BeautifulSoup的使用
- 简单爬虫python实现02——BeautifulSoup的使用
- Python爬虫:用BeautifulSoup进行NBA数据爬取
- 使用python进行爬虫下载指定网站的图片
- 关于使用Python3进行网络爬虫的字符问题
- Python从零开始写爬虫(二)BeautifulSoup库使用
- python的【爬虫】:使用urllib爬取wiki文章,使用beautifulSoup解析html
- python爬虫——beautifulsoup4使用学习
- python爬虫爬取指定用户微博图片及内容,并进行微博分类及使用习惯分析,生成可视化图表
- python3实现网络爬虫(6)--正则表达式和BeautifulSoup配合使用