Loading

bs4用法

bs4的作用

1、从html或者xml中提取数据的python库,修改xml

requests+bs4爬汽车之家新闻

######
#2  爬取汽车之家新闻
######
 
import requests
 
# 向汽车之家发送get请求,获取到页面
ret=requests.get('https://www.autohome.com.cn/news/1/#liststart')
# ret.encoding='gb2312'  # 设置编码为gb2312
# print(ret.text)
 
# bs4解析(不用re了)
# 安装 pip3 install beautifulsoup4
# 使用
from bs4 import BeautifulSoup
# 实例化得到对象,传入要解析的文本,解析器
# html.parser内置解析器,速度稍微慢一些,但是不需要装第三方模块
# lxml:速度快一些,但是需要安装 pip3 install lxml
soup=BeautifulSoup(ret.text,'html.parser')
# soup=BeautifulSaoup(open('a.html','r'))
# find(找到的第一个)
# find_all(找到的所有)
# 找页面所有的li标签
li_list=soup.find_all(name='li')
for li in  li_list:
    # li是Tag对象
    # print(type(li))
    h3=li.find(name='h3')
    if not h3:
        continue
 
    title=h3.text
    desc=li.find(name='p').text
    # 对象支持[]取值,为什么?重写了__getitem__魔法方法
    # 面试题:你使用过的魔法方法?
    img=li.find(name='img')['src']# type:str
    url=li.find(name='a')['href']
    # 图片下载到本地
    ret_img=requests.get('https:'+img)
    img_name=img.rsplit('/',1)[-1]
    with open(img_name,'wb') as f:
        for line in ret_img.iter_content():
            f.write(line)
    print('''
    新闻标题:%s
    新闻摘要:%s
    新闻链接:%s
    新闻图片:%s
    '''%(title,desc,url,img))

遍历文档树

from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"id="id_p"><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>
"""
# pip3 install lxml
soup=BeautifulSoup(html_doc,'lxml')
# 美化
# print(soup.prettify())
 
# 遍历文档树
# 1、用法(通过.来查找,只能找到第一个)
# Tag对象
head=soup.head
title=head.title
print(head)
print(title)
 
p=soup.p
print(p)
 
# 2、获取标签的名称
# Tag对象
p=soup.body
print(type(p))
from  bs4.element import Tag
print(p.name)
 
# 3、获取标签的属性
p=soup.p
# 方式一
# 获取class属性,可以有多个,拿到列表
print(p['class'])
print(p['id'])
print(p.get('id'))
# 方式二
print(p.attrs['class'])
print(p.attrs.get('id'))
 
 
# 4、获取标签的内容
p=soup.p
print(p.text) # 所有层级都拿出来拼到一起
print(p.string) # 只有一层,才能去除
print(list(p.strings)) # 把每层都取出来,做成一个生成器
 
#5、嵌套选择
title=soup.head.title
print(title)
 
# 6、子节点、子孙节点
p1=soup.p.children   # 迭代器
p2=soup.p.contents  # 列表
print(list(p1))
print(p2)
 
# 7、父节点、祖先节点
p1=soup.p.parent  # 直接父节点
p2=soup.p.parents
print(p1)
print(len(list(p2)))
print(list(p2))
 
# 8、兄弟节点
print(soup.a.next_sibling) #下一个兄弟
print(soup.a.previous_sibling) #上一个兄弟
 
print(list(soup.a.next_siblings)) #下面的兄弟们=>生成器对象
print(soup.a.previous_siblings) #上面的兄弟们=>生成器对象

查找文档树

查找文档树的速度比遍历文档树慢

查找文档树语法:find,find_all
 
# 因为速度慢,但可以配合遍历方法使用:soup.p.find()---先遍历P标签,再使用查找方法
# 五种过滤器: 字符串、正则表达式、列表、True、方法
# 以find为例
 
# 1 字符串查找 引号内是字符串
p=soup.find(name='p')
p=soup.find(name='body')
print(p)
# 查找类名是title的所有标签,class是关键字,class_
ret=soup.find_all(class_='title')
# href属性为http://example.com/elsie的标签
ret=soup.find_all(href='http://example.com/elsie')
# 找id为xx的标签
ret=soup.find_all(id='id_p')
print(ret)
 
# 2 正则表达式
import re
reg=re.compile('^b')
ret=soup.find_all(name=reg)
# 找id以id开头的标签
reg=re.compile('^id')
ret=soup.find_all(id=reg)
print(ret)
 
# 3 列表
ret=soup.find_all(name=['body','b'])
ret=soup.find_all(id=['id_p','link1'])
ret=soup.find_all(class_=['id_p','link1'])
# and 关系
ret=soup.find_all(class_='title',name='p')
print(ret)
 
 
# 4 True
# 所有有名字的标签
ret=soup.find_all(name=True)
# 所有有id的标签
ret=soup.find_all(id=True)
# 所有有herf属性的
ret=soup.find_all(href=True)
print(ret)
 
# 5 方法
def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
 
print(soup.find_all(has_class_but_no_id))
 
# 6 其他使用
ret=soup.find_all(attrs={'class':"title"})
ret=soup.find_all(attrs={'id':"id_p1",'class':'title'})
print(ret)
 
# 7 拿到标签,取属性,取text
ret=soup.find_all(attrs={'id':"id_p",'class':'title'})
print(ret[0].text)
 
# 8 limit(限制条数)
soup.find()  就是find_all limit=1
ret=soup.find_all(name=True,limit=2)
print(len(ret))
 
# 9 recursive
recursive=False (只找儿子)不递归查找,只找第一层
ret=soup.body.find_all(name='p',recursive=False)
print(ret)

搭建免费的代理池

github项目地址:https://github.com/jhao104/proxy_pool

# 收费的:提供给你一个接口,每掉一次这个接口,获得一个代理
# 免费:用爬虫爬取,免费代理,放到我的库中,flask,django搭一个服务(删除代理,自动测试代理可用性),每次发一个请求,获取一个代理
 
# 带你配置
 
# 1 下载,解压,用pycharm打开
# 2 安装依赖 pip install -r requirements.txt
# 3 配置Config/setting.py:
    DB_TYPE = getenv('db_type', 'redis').upper()
    DB_HOST = getenv('db_host', '127.0.0.1')
    DB_PORT = getenv('db_port', 6379)
    DB_PASSWORD = getenv('db_password', '')
# 4 本地启动redis-server
 
# 5 可以在cli目录下通过ProxyPool.py
  -python proxyPool.py schedule :调度程序,他会取自动爬取免费代理
  -python proxyPool.py webserver:启动api服务,把flask启动起来

验证码破解

# 1 简单验证码,字母,数字
# 2 高级的,选择,你好,12306选择乒乓球,滑动验证(极验)
 
# 打码平台(自动破解验证码,需要花钱)云打码,超级鹰(12306)
http://www.yundama.com/
http://www.chaojiying.com/
 
# 注册账号,(充钱)把demo下载下来,运行即可

爬取糗事段子,自动通过微信发送给女朋友

 
 
#####
# 1 爬取糗事百科,微信自动发送
#####
# https://www.qiushibaike.com/text/
# https://www.qiushibaike.com/text/page/1/
 
import requests
from bs4 import BeautifulSoup
ret=requests.get('https://www.qiushibaike.com/text/page/1/')
# print(ret.text)
ll=[]
soup=BeautifulSoup(ret.text,"lxml")
article_list=soup.find_all(name='div',id=True,class_='article')
for article in article_list:
    content=article.find(name='div',class_='content').span.text
    # content=article.find(name='div',class_='content').text
    # content=article.find(class_='content').text
    # print(content)
    # 入库
    #我们放到列表中
    ll.append(content)
print(ll)
 
# 微信自动发消息
# wxpy:实现了web微信的接口
# pip3 install wxpy
from wxpy import *
# 实例化得到一个对象,微信机器人对象
import random
bot=Bot(cache_path=True)
 
girl_friend=bot.search('姓名')[0]
print(girl_friend)
 
@bot.register() # 接收从指定好友发来的消息,发送者即recv_msg.sender为指定好友girl_friend
def recv_send_msg(recv_msg):
    print('收到的消息:', recv_msg.text) # recv_msg.text取得文本
    if recv_msg.sender == girl_friend:
        recv_msg.forward(bot.file_helper, prefix='老婆留言:')  # 在文件助手里留一份
        ms = '老婆最美丽,我对老婆的爱如滔滔江水,连绵不绝'
        # return ms
        return random.choice(ll)
 
embed()
posted @ 2020-04-22 20:43  开花的马铃薯  阅读(330)  评论(0编辑  收藏  举报