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)
注意点:
1.BeautifulSoup在地址后面要加参数
2.BS的引用方式
 
更多有关BS的教程:http://cuiqingcai.com/1319.html

4.raw_input() 与 input()

raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。

而对于 input() ,它希望能够读取一个合法的 python 表达式,

即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。

一般若无特殊需求,多用raw_input()

input() 可接受合法的 python 表达式,input( 1 + 3 ) 会返回 int 型的 4

5.BS的高级用法(课后作业1)

对上面网址中的comments求和
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)的更多相关文章

  1. 《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 ...

  2. Python Web-第二周-正则表达式(Using Python to Access Web Data)

    0.课程地址与说明 1.课程地址:https://www.coursera.org/learn/python-network-data/home/welcome 2.课程全名:Using Python ...

  3. 【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 ...

  4. 【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 ...

  5. 《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 ...

  6. 《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 ...

  7. 《Python Web开发实战》|百度网盘免费下载|Python Web开发

    <Python Web开发实战>|百度网盘免费下载|Python Web开发 提取码:rnz4 内容简介 这本书涵盖了Web开发的方方面面,可以分为如下部分: 1. 使用最新的Flask ...

  8. 2003031121-浦娟-python数据分析第四周作业-第二次作业

    项目 内容 课程班级博客链接 20级数据班(本) 作业链接 Python第四周作业第二次作业 博客名称 2003031121-浦娟-python数据分析第四周作业-matolotlib的应用 要求 每 ...

  9. Python从菜鸟到高手(1):初识Python

    1 Python简介 1.1 什么是Python   Python是一种面向对象的解释型计算机程序设计语言,由荷兰人吉多·范罗苏姆(Guido van Rossum)于1989年发明,第一个公开发行版 ...

随机推荐

  1. 基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合

    在上一篇<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>中完成了使用JPA对实体数据的CRUD操作. 那么,有些情况,会把一些查询语句写在存储过程中,由 ...

  2. 【Oracle】-初识PL/SQL

    在最近的工作中要用到存储过程和函数,索性把PL/SQL整体的看一下.之前看过基本书和园子里的博文,在这里将所学简单总结. 一.基本语句 1.大小写 2.分隔符  --  : 3.引用字符串  --   ...

  3. [Python Study Notes]计算cpu使用率v0.1

    V0.1 更新日志: 1.加入平台判断,支持windows与linux 2.自动清屏显示,显示更加直观 '''''''''''''''''''''''''''''''''''''''''''''''' ...

  4. 一步一步配置ABP Core Template with Angular

    1.首先去https://aspnetboilerplate.com/Templates下载模板工程,按如下勾选 2.下载后打开工程如下图,并设置Web.host 作为启动项目,rebuild 还原n ...

  5. POI导出EXCEL,浏览器不兼容,文件名称乱码,文件无法打开解决方法

  6. wordpress安装五步法

    原文链接: 下载并解压缩WordPress程序安装包 在你的网页服务器上为WordPress创建一个数据库, 并且创建一个MySQL 拥有所有权限可以进入和修改的用户 重命名 wp-config-sa ...

  7. 终极解决方案:java.security.cert.CertificateException: Certificates does not conform to algorithm constraints

    报错信息 javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: Certificates does ...

  8. 解决cookies存储中文报错问题

    URLEncoder.encode("username", "UTF-8"); URLDecoder.decode("123", " ...

  9. textarea只允许上下调节尺寸

    对于extarea,因为如果不做限制的话,它是可以自由调节尺寸的,对于这一点我相信用户是非常喜欢的,因为每个人都有自己认为合适的尺寸,但是对于前端来说就比较头疼了,因为随意调节宽高,就会破坏整体布局, ...

  10. (转载,但不知道谁原创)获取SPRING 代理对象的真实实例,可以反射私有方法,便于测试

    /** * 获取 目标对象 * @param proxy 代理对象 * @return * @throws Exception */ public static Object getTarget(Ob ...