python网络爬虫实战笔记(一)
来源:互联网 发布:seo必备工具 编辑:程序博客网 时间:2024/05/16 23:48
# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import requestsres = requests.get('http://news.sina.com.cn/china/')res.encoding = 'utf-8'#print(res.text)from bs4 import BeautifulSouphtml_sample = '\<html> \<body> \<h1 id ="title">Hello World</h1> \<a href="#" class="link">This is link1</a> \</body> \<html>'soup = BeautifulSoup(html_sample,'html.parser')print(soup.text)#使用select找出含有h1标签的元素soup = BeautifulSoup(html_sample,'html.parser')header = soup.select('h1')print(header)# 如果要把里面的元素取出来加中括号0print(header[0])# 如果仅要文字print(header[0].text)# 使用select找出含有a标签的元素soup = BeautifulSoup(html_sample,'html.parser')alink = soup.select('a')print(alink)#里面包含两个元素for link in alink:#把两个元素分别在两行打印出print(link)#仅取出文字print(link.text)# 取得含有特定css属性的元素# 使用select找出所有id为title的元素(id前面需加#)alink = soup.select('#title')print(alink)#使用select找出所有class为link的元素(class前面需要加.)for link in soup.select('.link'):print(link)# 取得所有a标签内的链接#使用select找出所有a tag的href连结alinks = soup.select('a')for link in alinks:print(link)print(link['href'])
阅读全文
0 0
- python网络爬虫实战笔记(一)
- Python实战(一)——Python编写网络爬虫
- Python网络爬虫学习笔记(一)
- Python爬虫实战(一)
- python爬虫实战(一)
- 【网络爬虫】【python】网络爬虫(一):python爬虫概述
- python网络爬虫(一)
- python网络爬虫(一)
- python网络爬虫(一)
- 学习笔记—Python网络爬虫(一)
- 精通python网络爬虫笔记一
- Python实战:Python网络爬虫
- python实战之网络爬虫
- WSWP(用python写网络爬虫)笔记 一:实现简单爬虫
- Python网络爬虫笔记
- Python爬虫入门实战一
- python学习笔记(一)爬虫实战:图片自动下载器
- 写网络爬虫学python(一)
- php 单引号和双引号的区别
- 凯撒加密
- Java Activiti(9)--网关
- java while循环写99乘法表
- 构造函数(方法)
- python网络爬虫实战笔记(一)
- C 数组名取地址
- C语言题目五
- Nginx平滑升级源码分析
- 购物车的实现思想
- ValueAnimator的初步使用(动画属性)
- 进程切换小结
- Codeforces 868F (Codeforces Round #438 F) Yet Another Minimization Problem DP+分治
- python list extend用法