美文网首页trivialPython程序员
一天就能写 Python 爬虫

一天就能写 Python 爬虫

作者: Nodelover | 来源:发表于2015-12-19 10:57 被阅读1878次

学习视频地址
代码参考(Python3.4)

成功截图

遇见了一些坑,特别是字符编码的问题,比较蛋疼,找了一下午。

基本流程

爬虫调度端 -> URL管理器 -> 网页下载器 -> 网页解析器  -> 有价值数据
              ↑                          |
              ↑--------------------------|

URL管理器

管理待抓取URL集合和已抓取集合

网页下载器

将网络上的HTML文件下载下来

第一种方式

import urllib2

# 直接请求
response = urllib2.urlopen('http://www.baidu.com')

# 获取状态码,如果是 200 表示获取成功
print response.getcode()

# 读取内容
cont = response.read()

第二种方式

import urllib2

# 创建 Request 对象
request = urllib2.Request(url)

# 添加数据
request.add_data('a','1')

# 添加 Http 的 Header
request.add_header('User-Agent','Mozilla/5.0')

# 发送请求获取结果
response = urllib2.urlopen(request)

第三种方式

import urllib2, cookielib

# 创建 Cookie 容器
cj = cookielib.CookieJar()

# 创建 1 个 opener
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

# 给 urllib2 安装 opener
urllib2.install_opener(opener)

# 使用带有 cookie 的 urllib2 访问网页
response = urllib2.urlopen('http://www.baidu.com')

网页解析器

  • 正则表达式
  • html.parser
  • Beautiful Soup
  • lxml

第一种是模糊匹配,后三者会解析成DOM树,再进行一些操作。

Beautiful Soup 文档

http://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
文档中显示例子在Python2.7和Python3.2中的执行结果相同,所以大家不用担心版本问题
使用命令安装 pip install beautifusoup4

简单使用

from bs4 import BeautifulSoup

soup = BeautifulSoup(
    html_doc,             # HTML 文档字符
    'html.parse'          # HTML 解析器
    from_encoding='utf8'  # HTML 文档的编码
)

# 查找所有标签为 a 的节点
soup.find_all('a')

# 查找所有标签为a,链接符合 /view/123.html 形式的节点
soup.find_all('a',href='/view/123.html')
soup.find_all('a',href=re.compile(r'/view/\d+\.html'))

# 查找所有标签为 div, class 为 abc ,文字为 Python 的节点
soup.find_all('div',class_='abc',string='Python')

# 假如得到节点 <a href="/categroy">Python</a>

# 获取查到节点的标签名称
node.name

# 获取查到节点的 href 属性
node['href']

# 获取查到链接的文字
node.get_text()
# 官网上的 demo
from bs4 import BeautifulSoup


html_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")

print(soup.find_all('a'))

相关文章

网友评论

  • _青菜_:爬虫的核心爬取动态内容完全没有,这虫完全只能玩玩
    Nodelover:@我不哇哈哈 是的,真正的生产环境还是用成熟的scrapy框架
  • 大象飞:您好 我在mac上安装MySQL 安装的是 5.7.11 版本的 也就是最新版本 安装完后 提示如下
    A temporary password is generated for root@localhost: dwstkti5xJ<5
    然后我就不知道怎么办了 要修改哪些配置呢 用户啦 路径啦 什么的
    目前到网上 找到 只做了如下两个操作 其余的还要弄哪些呢?
    alias mysql=/usr/local/mysql/bin/mysql
    alias mysqladmin=/usr/local/mysql/bin/mysqladmin
    Nodelover:@大象飞 MySQLWorkbench、SequelPro 自己去尝试吧。连接的时候记得先打开,官网安装的,在设置里面有mysql选项。因为我看你是安装在/usr/local下面的,所以应该是官网安装的。
    Nodelover:@Yugo 实在不会就用图形界面。官方有,还有其他的。很多。
    Nodelover:@大象飞 拿到密码,用命令登录到你的数据库呗。mysql -u root -p

本文标题:一天就能写 Python 爬虫

本文链接:https://www.haomeiwen.com/subject/ggnphttx.html