《Python网络数据采集》笔记之BeautifulSoup

news/2024/7/19 9:05:58 标签: python, 爬虫

一  初见网络爬虫

都是使用的python3。

一个简单的例子:

from  urllib.request import urlopen
html = urlopen("http://pythonscraping.com/pages/page1.html")
print(html.read())

在 Python 2.x 里的 urllib2 库, 在 Python 3.x 里,urllib2 改名为 urllib,被分成一些子模块:urllib.request、 urllib.parse 和 urllib.error。

 

二  BeautifulSoup

        1.使用BeautifulSoup

注意:1.通过pip install  BeautifulSoup4 安装模块

           2. 建立可靠的网络连接,能处理程序可能会发生的异常

如下面这个例子:

from urllib.error import HTTPError
from urllib.request import urlopen
from  bs4 import BeautifulSoup


def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:
        return None
    try:
        bsobj = BeautifulSoup(html.read())
        title = bsobj.body.h1
    except AttributeError as e:
        return None
    return title
title = getTitle("http://pythonscraping.com/pages/page1.html")
if title == None:
    print("title was not found")
else:
    print(title)

 

        2. 网络爬虫可以通过 class 属性的值,获得指定的内容

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://pythonscraping.com/pages/warandpeace.html")

bsobj = BeautifulSoup(html)

# 通过bsobj对象,用fillAll函数抽取class属性为red的span便签
contentList = bsobj.findAll("span",{"class":"red"})

for content in contentList:
    print(content.get_text())
    print('\n')

 

        3. 通过导航树

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://pythonscraping.com/pages/page3.html")
bsobj = BeautifulSoup(html)


#找出子标签
for child in bsobj.find("table",{"id":"giftList"}).children:
    print(child)

#找出兄弟标签
for silbling in bsobj.find("table",{"id":"giftList"}).tr.next_siblings:
    print(silbling)

for h2title in bsobj.findAll("h2"):
     print(h2title.get_text())

print(bsobj.find("img",{"src":"../img/gifts/img1.jpg"}).parent.previous_sibling.get_text())

 

        5. 正则表达式和BeautifulSoup

from urllib.request import urlopen
from bs4 import BeautifulSoup
import  re

html = urlopen("http://pythonscraping.com/pages/page3.html")
bsobj = BeautifulSoup(html)
#返回字典对象images
images = bsobj.findAll("img",{"src":re.compile("\.\.\/img\/gifts/img.*\.jpg")})
for image in images:  
    print(image["src"])

 

 

 

 

转载于:https://www.cnblogs.com/xiangshigang/p/7224941.html


http://www.niftyadmin.cn/n/842768.html

相关文章

Ticket Lock的Relaxed Atomics优化

Tick lock是mutual lock的一种简单实现: http://web.mit.edu/6.173/www/currentsemester/readings/R06-scalable-synchronization-1991.pdf 它是由John Mellor-Crummey和Michael Scott在1991年提出的(pdf中2.2节),感谢C11和C11中新…

php商品大小单位转换,php字母大小如何转换

php字母大小如何转换网站开发中有很多的数据需要具有规则性,方便于管理员进行管理,所以,在一些存储数据的提交请求时,对大小写是要要求的。我们为大家收集整理了关于php字母大小如何转换,以方便大家参考。1.将字符串转…

智能安防还是个非垄断的分散大市场

近年来,随着计算机视觉技术的提升,安防领域开始逐步发生变化。 国内计算机视觉技术创业热潮中,目前能找到的可落地的应用场景还不多,安防、支付是目前已经被验证的两个方向,国内从人脸识别起家的计算机视觉公司&#x…

java泛型中T、E、K、V、?等含义

2019独角兽企业重金招聘Python工程师标准>>> java泛型中T、E、K、V、?等含义 E - Element (在集合中使用,因为集合中存放的是元素),E是对各方法中的泛型类型进行限制,以保证同一个对象调用不同的方法时,操作…

php 将ip地址转为int,java_使用Java代码将IP地址转换为int类型的方法,基本知识点    IP —— - phpStudy...

使用Java代码将IP地址转换为int类型的方法基本知识点IP ——> 整数&#xff1a;把IP地址转化为字节数组通过左移位(<整数 ——> IP&#xff1a;将整数值进行右移位操作(>>>)&#xff0c;右移24位&#xff0c;再进行与操作符(&)0xFF&#xff0c;得到的数字…

从0开始安装fedora23的笔记-- 以及使用fedora的常规问题-3

关于sys的目录有: /etc/sys/, 和 /proc/sys fedora的桌面背景图片默认的在: /usr/share/backgrounds/, 里面有f23, gnome, images等目录, 其中设置gnome目录就是背景中的 Wallpapers. 注意区分 background 和 displays 两个设置项的区别: background是Personal 个人化的设置, 是…

基于 React 的前端项目开发总结

原文发表于【抹桥的博客-基于 React 的前端项目开发总结】 技术选型 我们的项目主要选用以下技术开发&#xff0c;再配合一些其它辅助工具。 reactreact-routerreduxreact-redux开发及线上环境 开发环境 由于项目是前后端分离的&#xff0c;所以我们需要一套完整的开发环境&…

Android零基础入门第13节:Android Studio配置优化,打造开发利器

2019独角兽企业重金招聘Python工程师标准>>> 是不是很多同学已经有烦恼出现了&#xff1f;电脑配置已经很高了&#xff0c;但是每次运行Android程序的时候就很卡&#xff0c;而且每次安装运行程序都要等待很长时间&#xff0c;如果是在开发后期需要不停的修改代码运…