Python Web-第四周-Programs that Surf the Web(Using Python to Access Web Data)
1.Understanding HTML
1.最简单的爬虫
import urllib
fhand=urllib.urlopen('http://www.dr-chuck.com/page1.htm')
for line in fhand:
print line.strip()
2.Python 爬网页和直接访问网页
3.Scrape
2.Parsing HTML with BeautifulSoup
1.这次直接使用简单方法 BeautifulSoup
2.BeautifulSoup的安装
1.下载 http://www.crummy.com/software/BeautifulSoup/#Download
2.将下载后的文件解压,并拷贝到C:Python27目录下
3.CMD cd到该目录下 运行 python setuyp.py install
3.初试BeautifulSoup(同样也是初试Python库)
import urllib
from bs4 importBeautifulSoup
url =raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup=BeautifulSoup(html,"html.parser")
tags = soup('a')
for tag in tags:
print tag.get('href',None)
4.raw_input() 与 input()
raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。
而对于 input() ,它希望能够读取一个合法的 python 表达式,
即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。
一般若无特殊需求,多用raw_input()
input() 可接受合法的 python 表达式,input( 1 + 3 ) 会返回 int 型的 4
5.BS的高级用法(课后作业1)
import urllib
from bs4 importBeautifulSoup
url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup =BeautifulSoup(html,"html.parser")
sc=soup.select('span[class="comments"]')#查找class为comments的span
Sum=0
Count=0
for span in sc:
# print 'span' ,span
# print 'Attr:' ,span.attrs
# print 'Contents:',span.contents[0]
Sum+=int(span.contents[0])#提取span中的内容
Count+=1
print'Count:',Count
print'Sum:',Sum
PS:
由于从Python 3 换成了 2 出现了 "Non-ASCII character" 问题
在源代码第一行添加:
#coding:utf-8
#-*- coding: UTF-8 -*-
Python Web-第四周-Programs that Surf the Web(Using Python to Access Web Data)的更多相关文章
- 《Using Python to Access Web Data》Week4 Programs that Surf the Web 课堂笔记
Coursera课程<Using Python to Access Web Data> 密歇根大学 Week4 Programs that Surf the Web 12.3 Unicod ...
- Python Web-第二周-正则表达式(Using Python to Access Web Data)
0.课程地址与说明 1.课程地址:https://www.coursera.org/learn/python-network-data/home/welcome 2.课程全名:Using Python ...
- 【Python学习笔记】Coursera课程《Using Python to Access Web Data》 密歇根大学 Charles Severance——Week6 JSON and the REST Architecture课堂笔记
Coursera课程<Using Python to Access Web Data> 密歇根大学 Week6 JSON and the REST Architecture 13.5 Ja ...
- 【Python学习笔记】Coursera课程《Using Python to Access Web Data 》 密歇根大学 Charles Severance——Week2 Regular Expressions课堂笔记
Coursera课程<Using Python to Access Web Data > 密歇根大学 Charles Severance Week2 Regular Expressions ...
- 《Using Python to Access Web Data》 Week5 Web Services and XML 课堂笔记
Coursera课程<Using Python to Access Web Data> 密歇根大学 Week5 Web Services and XML 13.1 Data on the ...
- 《Using Python to Access Web Data》 Week3 Networks and Sockets 课堂笔记
Coursera课程<Using Python to Access Web Data> 密歇根大学 Week3 Networks and Sockets 12.1 Networked Te ...
- 《Python Web开发实战》|百度网盘免费下载|Python Web开发
<Python Web开发实战>|百度网盘免费下载|Python Web开发 提取码:rnz4 内容简介 这本书涵盖了Web开发的方方面面,可以分为如下部分: 1. 使用最新的Flask ...
- 2003031121-浦娟-python数据分析第四周作业-第二次作业
项目 内容 课程班级博客链接 20级数据班(本) 作业链接 Python第四周作业第二次作业 博客名称 2003031121-浦娟-python数据分析第四周作业-matolotlib的应用 要求 每 ...
- Python从菜鸟到高手(1):初识Python
1 Python简介 1.1 什么是Python Python是一种面向对象的解释型计算机程序设计语言,由荷兰人吉多·范罗苏姆(Guido van Rossum)于1989年发明,第一个公开发行版 ...
随机推荐
- qt的信号与槽函数
关联: bool connect ( const?QObject?*?sender, const?char?*?signal, const QObject * receiver, const char ...
- yii2 源码分析 Component类分析 (二)
转载请注明链接http://www.cnblogs.com/liuwanqiu/p/6739538.html 组件(component),是Yii框架的基类,实现了属性.事件.行为三类功能,它集成自o ...
- golang的GET请求(类似于PHP的CURL)
check_url := "https://www.baidu.com" header := make(map[string]string) res, err := util.Hp ...
- python[error] - mysql_config not found
具体报错信息: root@pts/4 $ pip install MySQL-python Collecting MySQL-python Using cached MySQL-python-1.2. ...
- LCA—倍增法求解
LCA(Least Common Ancestors) 即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 常见解法一般有三种 这里讲解一种在线算法-倍增 首先我们定义fa[u][j ...
- 【echarts3】--1 简单入门
echarts3 相信大家都了解吧,是百度研发的 ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8 ...
- Python自动化--语言基础6--模块操作之re、MySQL、Excel
1.Python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("aaa ...
- 正则表达式的方法:replace,match,test(replace参数可以是回调函数)
1.replace: 作用对象:字符串 功能:用于替换字符串中的某些字符 参数:(1)正则表达式 (2)要替换的字符串 或者 回调函数 var str="1 2 3 4 5 6 7 8 9& ...
- hdu3480 Division(dp平行四边形优化)
题意:将n个数分成m段,每段的代价为最大值减最小值的平方,为代价最小是多少n<=10000 ,m<=5000 题解:先拍好序,从小到大,这样绝对是花费最小的,不过怎么样来做呢?一定很容易想 ...
- 用batch调用DB2 CLPPlus执行多个SQL文
不啰嗦直接上技能. 大概三部分组成: 1.bat文件.(run.bat) 2.辅助SQL文.(AllRun.sql) 3.要执行的SQL文.(S1.sql,S2.sql,S3.sql) +++++++ ...