自动补全代码:

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. Mysql读写分离实例

    吐槽:前天刚加完MQ,这回加读写分离.我也是醉了,但是弄完之后,就发现,似乎没我想的那么复杂,真的!另外,昨天试了一下用swagger编写API文档,太方便了,加上Mock service测试.这两天 ...

  2. JDBC 学习笔记(三)—— JDBC 常用接口和类,JDBC 编程步骤

    1. JDBC 常用接口和类 DriverManager 负责管理 JDBC 驱动的服务类,程序中主要的功能是获取连接数据库的 Connection 对象. Connection 代表一个数据库连接对 ...

  3. iOS开发 UILabel实现自适应高宽

    UILabel是iOS开发常用的控件.UILabel的属性需要了解,UILabel的特殊显示效果也需要我们掌握.UILabel自适应高宽度是很多初学者遇到的技术性难题.比如段文字,要让他完全地分行显示 ...

  4. [USACO12MAR]花盆Flowerpot (单调队列,二分答案)

    题目链接 Solution 转化一下,就是个单调队列. 可以发现就是一段区间 \([L,R]\) 使得其高度的极差不小于 \(d\) ,同时满足 \(R-L\) 最小. 然后可以考虑二分然后再 \(O ...

  5. PHP分页类(较完美)

    <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $lis ...

  6. Fedora 14 安装完后的设置 添加源 更新软件

    1.添加源:   (1)上海交大源:应用程序->系统工具->终端 输入 su    输入密码 输入   nano /etc/yum.repos.d/sjtu.repo 辅之以下内容: [F ...

  7. docker基础——自定义镜像、创建私有仓库、查看 docker 运行状态

    一.自定义镜像 1,案例1 要求:请自定义一个 docker 镜像,基于 hub.c.163.com/library/centos,要求创建出来的镜像在生成容器的时候,可以直接使用 ifconfig ...

  8. DispatcherServlet与ContextLoaderListener的对比

    1. 从DispatcherServlet和ContextLoaderListener的初始化过程可以看出,二者分别会生成一个WebApplicationContext,且以不同的attrName注册 ...

  9. SharePoint中使用Global.asax

    Global.asax是ASP.Net应用程序的一个文件,用来处理Application级别的事情.可以添加自定义代码到这个文件,详细使用方式见 http://msdn.microsoft.com/e ...

  10. 使用gcc的-finstrument-functions选项进行函数跟踪【转】

    转自:http://blog.csdn.net/jasonchen_gbd/article/details/44044899 版权声明:本文为博主原创文章,转载请附上原博链接. GCC Functio ...