自动补全代码:

import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
print(soup.prettify())#如果html代码补全,则自动补全
print(soup.title.string)

查找标签

#基本使用
soup.title#<title>xxxxxxx</title>
soup.title.string#xxxxxx

获取名称

#基本使用
soup.title#<title>xxxxxxx</title>
soup.title.name#title

获取属性

#基本使用
soup.a#<a>xxxxxxx</a>
soup.a['name']#a标签的name属性值

获取内容

soup.title.string#xxxxxx

嵌套选择

print(soup.head.title.string)

子节点

import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
print(soup.div.contents)

或者

import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
a=soup.div.children
print(a)
for i,j in enumerate(a):
print(i,j)

子孙节点

import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
a=soup.div.descendants
print(a)
for i,j in enumerate(a):
print(i,j)

获取父节点

import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
a=soup.div.parent
print(a)

获取祖先节点

import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
a=soup.div.parents
for i,j in enumerate(a):
print(i,j)

获取兄弟节点

a=soup.div.next_siblings#后面的兄弟节点(迭代器)

前面的兄弟节点

a=soup.div.next_siblings#前面的兄弟节点(迭代器)

标准选择器
find_all(name,attrs,recursive,**kwargs)

name

import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
print(soup.find_all('ul'))#根据标签名查找
import requests
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
for ul in soup.find_all('ul'):
for li in ul.find_all('li'):
print(li)

attrs

import requests
import re
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
a=soup.find_all(attrs={'class':'lazy'})#<a class='lazy'>xxxxx</lazy>
for index,i in enumerate(a):
result=re.findall(r'[a-zA-z]+://[^\s]*png',str(i))
url=result[0]
res = requests.get(url)
with open('%d.png'%index,'wb')as f:
f.write(res.content)
import requests
import re
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
a=soup.find_all(class_='lazy')
a=soup.find_all(id='lazy')

CSS选择器

import requests
import re
from bs4 import BeautifulSoup
response=requests.get('https://www.ithome.com/html/it/340684.htm',timeout=9)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
a=soup.select('.lazy') for index,i in enumerate(a):
result=re.findall(r'[a-zA-z]+://[^\s]*png',str(i))
url=result[0]
res = requests.get(url)
with open('./test/%d.png'%(index+1),'wb')as f:
f.write(res.content)

获取css属性

import requests
import re
from bs4 import BeautifulSoup
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
}
response=requests.get('https://www.zhihu.com/question/20519068/answer/215288567',headers=headers,timeout=None)
result=response.text
soup=BeautifulSoup(response.content,'lxml')
# print(soup.prettify())
a=soup.select('.lazy')
# print(a)
for index,i in enumerate(a):
url=i['data-original']
#
   #result=re.findall(r'[a-zA-z]+://[^\s]*jpg',str(i))
# url=result[0]
res = requests.get(url)
with open('./test/%d.jpg'%(index+1),'wb')as f:
f.write(res.content)

获取内容

li.get_text()

beautifulsoup的一些使用的更多相关文章

  1. Python爬虫小白入门(三)BeautifulSoup库

    # 一.前言 *** 上一篇演示了如何使用requests模块向网站发送http请求,获取到网页的HTML数据.这篇来演示如何使用BeautifulSoup模块来从HTML文本中提取我们想要的数据. ...

  2. 使用beautifulsoup与requests爬取数据

    1.安装需要的库 bs4 beautifulSoup  requests lxml如果使用mongodb存取数据,安装一下pymongo插件 2.常见问题 1> lxml安装问题 如果遇到lxm ...

  3. BeautifulSoup :功能使用

    # -*- coding: utf-8 -*- ''' # Author : Solomon Xie # Usage : 测试BeautifulSoup一些用法及容易出bug的地方 # Envirom ...

  4. BeautifulSoup研究一

    BeautifulSoup的文档见 https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ 其中.contents 会将换行也记录为一个子节 ...

  5. BeautifulSoup

    参考:http://www.freebuf.com/news/special/96763.html 相关资料:http://www.jb51.net/article/65287.htm 1.Pytho ...

  6. BeautifulSoup Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.

    BeautifulSoup很赞的东西 最近出现一个问题:Python 3.3 soup=BeautifulSoup(urllib.request.urlopen(url_path),"htm ...

  7. beautifulSoup(1)

    import re from bs4 import BeautifulSoupdoc = ['<html><head><title>Page title</t ...

  8. python BeautifulSoup模块的简要介绍

    常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...

  9. BeautifulSoup 的用法

    转自:http://cuiqingcai.com/1319.html Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python ...

  10. BeautifulSoup的选择器

    用BeautifulSoup查找指定标签(元素)的时候,有几种方法: soup=BeautifulSoup(html) 1.soup.find_all(tagName),返回一个指定Tag元素的列表 ...

随机推荐

  1. GUI——AWT框架和容器,创建简单窗体

    GUI概述:GUI(Graphical User Interface)—图形化用户界面.用户和程序之间可以通过GUI能方便友好地进行交互,在Java语言中,JFC(Java Foundation Cl ...

  2. Tomcat自动发布war包

    有两种方法: 1.将项目打成war包,复制到${tomcat.home}\webapps目录下.当tomcat启动时会自动将其解包. 有人说,不能直接将war文件夹直接复制到${tomcat.home ...

  3. BZOJ 4821 [Sdoi2017]相关分析 ——线段树

    打开题面,看到许多$\sum$ woc,好神啊,SDOI好强啊 然后展开之后,woc,SDOI好弱啊,怎么T3出个线段树裸题啊. 最后写代码的时候,woc,SDOI怎么出个这么码农的题啊,怎么调啊. ...

  4. NOIP赛前模拟20171027总结

    题目: 1.寿司 给定一个环形的RB串··要求经过两两互换后RB分别形成两段连续区域,求最少操作次数(算法时间O(n)) 2.金字塔 给定一个金字塔的侧面图有n层··已知每一层的宽度··高度均为1·· ...

  5. ecs01初始化node环境

    npm install 报错 > uglifyjs-webpack-plugin@ postinstall /opt/apps/iview-admin/node_modules/webpack/ ...

  6. Visual Code 自定义插件安装位置

    修改快速方式通过传入方式启动 ,示例: D:\installed_green_soft\VSCode\Code.exe --extensions-dir "D:\installed_gree ...

  7. FZOJ Problem 2148 Moon Game

                                                                                                  Proble ...

  8. 【NOIP2016】换教室(DP,期望)

    题意: 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 i ( 1≤ i≤n)个时同段上, 两节内容相同的课程 ...

  9. iOS键盘中英文切换键盘高度获取通知方法

    iOS键盘中英文切换键盘高度获取通知方法, 有需要的朋友可以参考下. 注册通知 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppea ...

  10. AC日记——【模板】线段树 1 洛谷 P3372

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个 ...