Python——爬虫(解析简单标签)
来源:互联网 发布:南京毓铭网络怎么样 编辑:程序博客网 时间:2024/05/16 09:34
Python——爬虫(解析简单标签)
# -*- coding: utf-8 -*-# 使用BeautifulSoup解析网页from bs4 import BeautifulSoup#获取要解析的标签with open('test.html','r',encoding='utf-8') as wb_data: Soup = BeautifulSoup(wb_data,'lxml'); #将要解析的文件传入 print(Soup); #打印读入Soup中的内容 print("--------------\n") images = Soup.select('body > img'); #将要解析的标签元素路径传入 urls = Soup.select('#binner > ul > li > a') #可以从网站上直接复制 print(images,urls,sep='\n---------------\n'); #打印解析标签元素包含内容wb_data.close();#解析标签内容-------使用get_text()获得文本内容,使用get('')方法获取标签属性值list = [];for image,url in zip(images,urls): data = { 'image':image.get_text(), 'image_src':image.get('src'), 'url':url.get_text(), 'url_href':url.get('href'), 'value':url.get('value') } list.append(data);for i in list: if float(i['value'])>2.0: print(i);
html文件:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><img src = "test1.png"/><img src = "test2.png"/><img src = "test3.png"/><div id = 'binner'><ul><li><a href = 'http://www.runoob.com/' value = '3.1'>菜鸟教程</a></li><li><a href = 'http://www.baidu.com/' value = '1.2'>百度主页</a></li><li><a href = 'http://www.sdu.edu.cn/' value = '2.4'>山东大学</a></li></ul></div></body></html>
上面是一个爬去本地html文件的实例,下面是爬取一个“真实”网站的例子:
# -*- coding: utf-8 -*-from bs4 import BeautifulSoupimport requestsurl = 'https://hao.360.cn/?a1004'wb_data = requests.get(url)soup = BeautifulSoup(wb_data.text,'lxml') #把web_data变得可读#解析网页元素,从网站上复制元素的CSS路径#这里以链接为例#famous-section > ul.list.first.gclearfix > li:nth-child(7) > aurl_famous = soup.select('#famous-section > ul.list.first.gclearfix > li > a')#famous-section > ul.list.last.gclearfix > li:nth-child(1) > aurl_famous .append(soup.select('#famous-section > ul.list.last.gclearfix > li > a'))print(url_famous)#focus_news > ul > li:nth-child(1) > aurl_focus = soup.select('#focus_news > ul > li > a')print(url_focus)
注意:使用beautifulsoup解析网页元素的时候,需要传入网页元素的CSS路径,可以通过审查元素然后右击获得
1 0
- Python——爬虫(解析简单标签)
- python爬虫解析标签
- Python爬虫——DNS解析缓存
- python爬虫——写出最简单的网页爬虫
- python开发简单爬虫——笔记(更新中)
- Python简单爬虫——淘宝数据
- python网络爬虫入门(一)——简单的博客爬虫
- python爬虫系列(1)——一个简单的爬虫实例
- python简单爬虫(二)
- 简单 python 爬虫(一)
- 一个简单的python爬虫程序+分词+标签云
- Python 3爬虫网易云(五)—— 正则表达式下篇之HTML标签
- python网络爬虫入门(二)——用python简单实现调用谷歌翻译
- Python写爬虫——抓取网页并解析HTML
- Python写爬虫——抓取网页并解析HTML
- Python写爬虫——抓取网页并解析HTML
- Python写爬虫——抓取网页并解析HTML
- Python写爬虫——抓取网页并解析HTML
- Android OkHttp完全解析 是时候来了解OkHttp了
- spring mvc详解
- MVC与WebForm的区别
- 1016. 部分A+B (15)
- CentOS Linux服务器安全设置
- Python——爬虫(解析简单标签)
- 【Webview相关问题】登陆失败之cookie陷阱
- 消除unchecked cast Warning
- 【SSH-商城】-框架介绍
- c++STL值assign
- 测试开发中遇到火狐浏览器自动升级问题如何解决?
- WPF中XML的读写
- Gym - 101102C C. Bored Judge 线段树+贪心+反向推
- php+mysql用户注册与验证页面代码