学习python网络数据采集笔记-1、2章

英文不好只能看中文版的。邮电出版社翻译的真很烂。

以上是吐槽,以下是正文。

书中用的pthon 3.X版本,建议安装python3.4以上的版本,低版本的没有自带pip安装插件会比较麻烦。

下载地址:https://www.python.org/downloads/windows/

1.1注意乌鸦处提示,如果用2.x的版本后面写urllib.request处替换成urllib或者urllib2.

1.2.1 安装包命令一定不要写错 pip install beatifulsoup4 

1.2.2 用html.read() 读取网页中ccs样式里的h1标签的内容

#! /usr/bin/env python
#coding=utf-8
from urllib.request import urlopen
#3.*版本是这样的,2.*去掉后面.request,参照1.1乌鸦处提示
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObj=BeautifulSoup(html.read())
print(bsObj.h1.get_text())

1.2.3设置报错

       网页不存在 except HTTPError as e:

       服务器不存在 if html is None

       属性错误: except AttributeError as e:

       如何创建函数,返回报错。

#! /usr/bin/env python
#coding=utf-8
from urllib2 import urlopen
from bs4 import BeautifulSoup

from urllib2 import HTTPError

def getTitle(url):
    try:
        html =urlopen(url)
    except HTTPError as e:
        #e为异常对象实例
        return None
    try:
        bsObj=BeautifulSoup(html.read())
        title=bsObj.body.h1
    except AttributeError as e:
        return None
    return title
title=getTitle("http://www.pythonscraping.com/pages/pageee1.html")
#这里指定一个无法找到的页面
if title == None:
    print("title could not be found")
else:
    print(title)

2.2根据标签属性抽取文字

namelist=bsObj.findAll("span",{"class":"green"}

#这里需要主要的是findAll中的A必须要大写。

get_text()是起到删除标签作用,可以将其添加print(bsObj.h1.get_text())中,运行删除h1标签

View Code

2.2.1 find和findAll的差别,可用limit限制findAll的寻找层数,具体差别出了limit限制完全没看明白

2.2.2 beautifulsoup的对象

  普通对象 bsObj

  标签Tag对象 bsObj.div.h1

  NAvigablesString对像  标签里面的文字

  Comment对象 查找注释文字<!--***-->

2.2.3导航树--子、兄弟、父标签

子标签(children)和后代标签(descendant)

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj=BeautifulSoup(html)
#比1.2中省略了.read()
for child in bsObj.find("table",{"id":"giftList"}).children:
    #.children是子对象,.descendants是所有后代
    print (child)

兄弟标签 

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj=BeautifulSoup(html)
for sibling in bsObj.find("table",{"id":"giftList"}).tr.next_siblings:
#.tr提取标题行
#.next_siblings提取除标题行外的数据
#.previous_siblings提取最后一行外的数据
#上面两个去掉s只返回单个标签
    print (sibling)

父标签

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj=BeautifulSoup(html)
print (bsObj.find("img",{"src":"../img/gifts/img1.jpg"}).parent.previous_sibling.get_text())
#翻译下来就是打印图片img1.jpg父亲的上级兄弟的删除标签结果。

2.3正则表达式。

此处延伸扩展就能单独一篇这里不多介绍,站长工具里面有正则表达式工具

2.4正则表达式和Beautifulsoup 

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bsObj=BeautifulSoup(html)
import re
images=bsObj.findAll("img",{"src":re.compile("../img/gifts/img.*.jpg")})
for image in images:
    print(image["src"])

2.5获取属性

介绍的太简单我也看不明白

2.6Lambda表达式

暂时没有接触过

2.7采集还有很多其他的之前用urllibe和urllibe2爬过微博

原文地址:https://www.cnblogs.com/Fsloth/p/5801521.html