网页下载器示例:

# coding:utf-8
import urllib2
import cookielib
url = "http://www.baidu.com"print u"第一种方法"
# pip install urllib2
response1 = urllib2.urlopen(url)
print response1.getcode()
print len(response1.read())print u"第二种方法"
request = urllib2.Request(url)
# 把爬虫伪装成浏览器
request.add_header("user-agent", "Mozilla/5.0")
response2 = urllib2.urlopen(request)
print response2.getcode()
print len(response2.read())print u"第三种方法"
# pip install cookielib
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(request)
print response3.getcode()
print cj
print len(response3.read())

# 运行结果

beautifulsoap使用示例

#coding:utf-8# 安装beautifulsoap4  D:\Python27\Lib>pip install beautifulsoup4from bs4 import BeautifulSoup
import rehtml_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p><p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p><p class="story">...</p>
"""soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')print u'获取所有的链接'
links = soup.find_all('a')for link in links:print link.name,link['href'], link.get_text()print u'获取lacie的链接'
link_node = soup.find('a', href='http://example.com/lacie')
print link_node.name, link_node['href'],link_node.get_text()print u'正则匹配'
link_node = soup.find('a', href=re.compile(r"ill"))
print link_node.name, link_node['href'],link_node.get_text()print u'获取p段落名字'
link_node = soup.find('p', class_="title")
print link_node.name, link_node.get_text()

转载于:https://www.cnblogs.com/reblue520/p/11083150.html

python爬虫简单架构原理及示例相关推荐

  1. python画折线图代码-python绘制简单折线图代码示例

    1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...

  2. python画折线图详解-python绘制简单折线图代码示例

    1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...

  3. python画折线图-python绘制简单折线图代码示例

    1.画最简单的直线图 代码如下: import numpy as np import matplotlib.pyplot as plt x=[0,1] y=[0,1] plt.figure() plt ...

  4. python爬虫下载小说_用PYTHON爬虫简单爬取网络小说

    用PYTHON爬虫简单爬取网络小说. 这里是17K小说网上,随便找了一本小说,名字是<千万大奖>. 里面主要是三个函数: 1.get_download_url() 用于获取该小说的所有章节 ...

  5. python爬虫简单练手:音乐top250

    python爬虫简单练手:音乐top250 使用的库bs4和requests: 通过requests发送网络请求 通过bs4的beautifulsoup解析html 页面分析: top250一共有10 ...

  6. 用PYTHON爬虫简单爬取网络小说

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 python免费学习资 ...

  7. python爬虫流程-Python爬虫程序架构和运行流程原理解析

    1 前言 Python开发网络爬虫获取网页数据的基本流程为: 发起请求 通过URL向服务器发起request请求,请求可以包含额外的header信息. 获取响应内容 服务器正常响应,将会收到一个res ...

  8. Pycharm + python 爬虫简单爬取网站数据

    本文主要介绍简单的写一个爬取网站图片并将图片下载的python爬虫示例. 首先,python爬虫爬取数据,需要先了解工具包requests以及BeautifulSoup requests中文文档:ht ...

  9. Python爬虫简单入门教程

    这篇 Python 爬虫教程主要讲解以下 5 部分 了解网页结构: 使用 requests 库抓取网站数据: 使用 Beautiful Soup 解析网页: 清洗和组织数据: 爬虫攻防战: 了解网页结 ...

最新文章

  1. 我想成为一名计算机管理员英语作文,我想成为一名老师英语作文4篇
  2. 一个Lex/Yacc完整的示例(可使用C++)
  3. 准备拉琴的zskame大白菜
  4. LeetCode 445 分发饼干
  5. -Android的发展webservice-号码归属地查询
  6. 在Ubuntu中安装以theano作为backend的keras
  7. PID控制电机输出作为电机PWM占空比输入的理解
  8. centos检测不到磁盘_CentOS下磁盘坏道的检测及修复
  9. 解密搭建平台赚钱的方法一:详解搭建网站收入的几个来源
  10. python导入模块不存在_基于pycharm导入模块显示不存在的解决方法
  11. web前端 第一次作业
  12. JavaScript获取法定节假日
  13. 触摸传递 Touch Delivery
  14. 去了字节跳动,才知道年薪 40w 的 90 后 Android 开发者有这么多?
  15. 一篇文章带你快速了解荧光蛋白
  16. 使用 IDEA 翻译插件
  17. android 调出键盘表情_Android 显示输入法中的emoji表情以及String字符
  18. ssm基于微信小程序的恋上诗词设计与实现毕业设计源码011431
  19. vue 根据链接生成二维码(功能实现)
  20. 华为APP UI自动化测试岗面试真题,真实面试经历。

热门文章

  1. npm -S -D -g i 有什么区别
  2. No.1 字符编码数据类型
  3. vitualbox的一个问题总结
  4. ECMAScript 发展历史
  5. [转载]数据库外键的使用
  6. tableView编辑
  7. -webkit-有意思的属性
  8. Aptana Studio 3 如何汉化,实现简体中文版
  9. html5 乱码解决方案
  10. 【python】整理的 Python 库