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年发明,第一个公开发行版 ...
随机推荐
- 网络编程之socketserver
网络编程之socketserver """ socketserver.py 中的5个基础类 +------------+ | BaseServer | +-------- ...
- Leetcode刷题C#版之 Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- asp.net Global.asax 不运行解决
asp.net application的站点发布后 Global.asax 未运行,搞了好久终于解决, 解决方法如下: publish设置 该设置经测试在win server 2003 和2008 都 ...
- 从零开始学Python--数据类型之字符串
一.Python中的数据类型 · 整数, 如 1 -100 · 长整数, 是比较大的整数,Python 2里面有long长整数:Python 3里面没有 · 浮点数 如 1.23.3E-2 ...
- Java经典编程题50道之四十六
编程实现两个字符串的连接. public class Example46 { public static void main(String[] args) { addString( ...
- Linux 安装nodejs环境以及路径配置
linux安装nodejs有2种方式一种简单的,解压即可用:另一种,通过下载source code ,通过编译,make,make install命令来安装. 这里只讲第一种,简单方便.不需要执行ma ...
- js的继承实现
1.原型链继承 1.创建父类对象2.创建子类函数对象3.将父类的实例对象赋值给子类的原型4.将子类的原型属性的构造函数设置为 子类本身 function Person(name) { this.nam ...
- linux中/bin和/sbin和/usr/bin和/usr/sbin
首先先解释一下bin和sbin的意思: 1.bin:为任何用户都可以使用的指令 2.sbin:(super bin)也就是只有超级管理员才能使用的指令 /usr UNIX Software Re ...
- css的一些复习
css,全称Cascading Style Sheets,层叠样式表. css选择器是从右往左解析的,解析速度会比较快. 选择器 选择器权重 !important 优先级最高 元素属性 优先级高 相同 ...
- MySQL 5.7 多主一丛同步的数据库配置(将多处数据源合并到一点)
工作需要,笔记之用.文章很长,倒一杯茶慢慢看. 数据库的应用场景颇多,如 数据库双机同步,一主多从,多主多从,多主一从等:下文记录多主一从的配置及测试. 大多数复制场景中是一主或者一主多从.这种拓扑用 ...