ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

BeautifulSoup 爬虫入门Ⅰ

2021-10-18 15:58:17  阅读:186  来源: 互联网

标签:入门 urlopen 标签 BeautifulSoup 爬虫 html bs import


BeautifulSoup4安装

pycharm 直接在setting 里面找到 Beautifulsoup4 install就好
注意:

  • interpreter 要知道是下在了哪一个
  • 编译时记得查看 edit configuration 的interpreter 是否对应

用 BeautifulSoup 简单爬一个对象

from urllib.request import urlopen
from bs4 import BeautifulSoup
html= urlopen('https://www.pythonscraping.com/pages/page1.html')
bs=BeautifulSoup(html.read(),'html.parser')
print(bs.h1)

网站可以换,h1 也可以换成其它标签

防止一些错误的方法

当找不到网页或者找不到网页所在服务器或者网页上的内容不是要找到 都会出现异常
下面函数能够检查包括URL输入错误的几个问题。

def getTitle(url):
    try:
        html = urlopen(url)
    except HTTPError as e:#网页不存在
            return None
    try:
        bs = BeautifulSoup(html.read(), 'html.parser')
        title=bs.body.h1
    except Attributerror as e:#URL输入错误和服务器不存在
            return None
            return title


title=getTitle('https://www.pythonscraping.com/pages/page1.html')
if title == None:#出现任何问题
    print('no paragraph')
else:
    print(title)

find 和 find_all

find(tag,attributrs,recursive,text,keywords)

find_all(tag,attributes,recursive,text,keywords)

tag就是HTML里面的标签
attributes 用dict 字典把标签和标签对应的值封装
recursive bool量 想要抓取多少层的标签信息
keyword 【tag是标签间“或”的方式获取】 keyword查找关键词 实现一个“与”的逻辑

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('https://www.pythonscraping.com/pages/page1.html')
bs = BeautifulSoup(html.read(),'html.parser')
nameL = bs.findAll('span', {'class' : 'green'})
for name in nameL:
    print(name.gte_text())
关于get_text()

函数会清除所有标签并只返回一个只包含文字的字符串‘(清楚其他的段落、超链接)

导航树 (navigating trees)

子标签与后代标签

子标签**.children()** 只显示后一层的node
后代标签 .descendants() 显示后面所有层的标签

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bs = BeautifulSoup(html,"html.parser")
for child in bs.find("table",{"id":"giftList"}).children:
    print(child)
兄弟标签

显示后面的兄弟标签 (不包括调用的自身giftList 标签)

# 前面部分一样的
for bro in bs.find("table",{"id":"giftList"}).tr.next_sibling:
    print(bro)

正则表达式regex

正则可以挑选符合表达式规范的字符串并返回
但具体表达式的构建有些复杂 贴了文件 需要就去查看

regexpal 网站

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bs = BeautifulSoup(html,"html.parser")
image=bs.find_all('img',
{'src':re.compile('\.\.\/img\/gifts\/img.*\.jpg')})#正则表达式
for img in image:
    print(img['src'])

Lambda表达式

进行函数的嵌套,一个函数作为变量(返回Bool型结果)套入另一个函数

html = urlopen("http://www.pythonscraping.com/pages/page3.html")
bs = BeautifulSoup(html,"html.parser")
image=bs.find_all(lambda tag: len(tag.attrs) == 2)
for img in image:
    print(img)

标签:入门,urlopen,标签,BeautifulSoup,爬虫,html,bs,import
来源: https://blog.csdn.net/Freyua_xx/article/details/120708125

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有