python爬虫入门——beautifulsoup初使用
来源:互联网 发布:童瑶的知乎回答 编辑:程序博客网 时间:2024/04/29 10:41
from《python网络数据采集》第一、二章
书上是python3的版本,而我电脑是python2.7,做了小修改
import urllib2import bs4def getTitle(url): try: html = urllib2.urlopen(url) except urllib2.HTTPError as e: return None try: bsObj = bs4.BeautifulSoup(html.read(), "lxml") title = bsObj.h5 except urllib2.AttributeError as e: return None return titletitle = getTitle("http://www.pythonscraping.com/pages/page1.html")if title == None: print("Title could not be found")else: print title
标签处理
import urllib2import bs4html = urllib2.urlopen("http://www.pythonscraping.com/pages/page3.html")bsObj = bs4.BeautifulSoup(html.read(), "lxml")#获取表格内容 children获取子标签for child in bsObj.find("table",{"id":"giftList"}).children: print(child)#获取除标题外的表格内容 next_siblings获取兄弟标签for sibling in bsObj.find("table",{"id":"giftList"}).tr.next_siblings: print(sibling) #获取父标签 parentparent = bsObj.find("img",{"src":"../img/gifts/img1.jpg"}).parent.previous_sibling.get_text()print(parent)
0 0
- python爬虫入门——beautifulsoup初使用
- Python 爬虫 —— BeautifulSoup
- python爬虫之BeautifulSoup入门
- 简单爬虫python实现02——BeautifulSoup的使用
- Python使用BeautifulSoup进行爬虫
- python网络爬虫-使用BeautifulSoup
- python爬虫——BeautifulSoup基础操作
- python爬虫——BeautifulSoup 抓取图片
- Python爬虫入门(三)BeautifulSoup库
- Python爬虫入门之一-requests+BeautifulSoup
- Python爬虫(2)--BeautifulSoup的使用
- python爬虫(1)——BeautifulSoup库函数find_all()
- Python爬虫实例——基于BeautifulSoup和requests实现
- python爬虫之BeautifulSoup
- python爬虫之-BeautifulSoup
- python beautifulsoup 爬虫学习
- python爬虫之BeautifulSoup
- python-爬虫-beautifulsoup
- OAF 系列教程 三 查询(下)
- 基于CEP的量化交易平台建设
- JDBC连接数据库
- 测试脚本
- 使用线程池(3)
- python爬虫入门——beautifulsoup初使用
- Android体系结构 - 了解
- 修改Oracle数据库用户的密码过期时间为无限期
- MySQL——常用SQL优化(四)
- C#76课的主要内容
- Leetcode: 18. 4Sum 四数之和
- cell高度自适应实现的最简单方式
- 十分经典的windows批处理教程
- Android调用系统摄像头拍照并显示在ImageView上