bs4forpython3
A. ubuntu16.04python怎么添加bs4包
1、Ctrl+Alt+T 打开命令终端,输入: vim –version |grep Python 查看vim是否支持python我这个vim只支持python3,不支持python。
2、安装py2包,在命令终端下输入: sudo apt-get install vim-nox-py2。
3、可以再次用vim –version|grep python 查看此时vim是否支持python,若支持到此为止,若不支持,请执行第四步。
4、在命令终端输入:sudo update-alternatives –config vim
我这里是第三项属于python,第二项属于python3,故想打开哪一项支持就输入它的编号就可以了(0,1,2,3)。
B. python bs4里的错误
原因可能出在你没有为Python3 安装模块。
可以试试 pip3 install bs4
或Linux 、mac 下用sudo pip3 install bs4
C. python3 用BeautifulSoup 爬取指定ul下的a标签
用select('ul的css路径').find_all(...)
css路径直接用浏览器开发视图,从ul复制就好,当然也可以把前面多余的部分删掉
D. python3如何安装bs4
在python官网找到beautifulsoup模块的下载页面,点击"downloap"将该模块的安装包下载到本地。
相关推荐:《Python教程》
将该安装包解压,然后在打开cmd,并通过cmd进入到该安装包解压后的文件夹目录下。
在该文件目录下输入"python install setup.py",进行beautifulsoup4模块的安装,当安装完成后会看到有"Finished"字样。
安装完成后,在cmd中运行Python,然后输入"from bs4 import BeautifulSoup" 导入该模块,如果成功安装的话将没有任何打印信息,否则会有相应的错误信息打印。
直接通过pip安装
打开cmd,然后在cmd中输入命令“pip install beautifulsoup4”,就可以成功安装beautifulsoup4,不过该版本不一定会是4.4.1,版本会是当前python库中的最新版本。
E. Python3.4怎么安装pip,lxml,bs4和requests求大神解答!!!Window
先下一个pip,再用命令指示符进入该目录,看到一个setup.py的文件 输入Python34 install setup.py 就OK👌😁
F. python/beautifulsoup4-4.1.3/目录下怎么操作
一、使用pip直接安装beautifulsoup4
F:/>pip install beautifulsoup4
Collecting Beautifulsoup4
Downloading beautifulsoup4-4.4.1-py3-none-any.whl (81kB)
50% |████████████████ | 40kB 33kB/s eta 0:00:
62% |████████████████████▏ | 51kB 32kB/s eta
75% |████████████████████████▏ | 61kB 39kB/s
88% |████████████████████████████▏ | 71kB 21k
100% |████████████████████████████████| 81kB
25kB/s
Installing collected packages: Beautifulsoup4
Successfully installed Beautifulsoup4-4.4.1
或者从官网下载Beautifulsoup的软件包,然后解压,cmd命令行进入解压包目录,输入以下命令安装:python setup.py install
记得在Python3里一定要安装beautifulsoup4的版本,其它版本安装不上的。
二、例子:
#python 3.4
#蔡军生 2016-6-13
#
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><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>
"""
soup = BeautifulSoup(html_doc, "html.parser")
print(soup.title)
print('*' * 80)
print(soup.title.name)
print(soup.title.string)
print(soup.p)
print(soup.a)
print(soup.find_all('a'))
print(soup.find(id='link3'))
print(soup.get_text())
>>>
<title>The Dormouse's story</title>
********************************************************************************
title
The Dormouse's story
<p class="title"><b>The Dormouse's story</b></p>
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
[<a
class="sister" href="http://example.com/elsie"
id="link1">Elsie</a>, <a class="sister"
href="http://example.com/lacie" id="link2">Lacie</a>, <a
class="sister" href="http://example.com/tillie"
id="link3">Tillie</a>]
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...
>>>
可以看出:soup 就是BeautifulSoup处理格式化后的字符串,soup.title 得到的是title标签,soup.p 得到的是文档中的第一个p标签,要想得到所有标签,得用find_all
函数。find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.
get_text() 是返回文本,这个对每一个BeautifulSoup处理后的对象得到的标签都是生效的。你可以试试 print(soup.p.get_text())
其实是可以获得标签的其他属性的,比如我要获得a标签的href属性的值,可以使用 print(soup.a['href']),类似的其他属性,比如class也是可以这么得到的(soup.a['class'])。
特别的,一些特殊的标签,比如head标签,是可以通过soup.head 得到,其实前面也已经说了。
如何获得标签的内容数组?使用contents 属性就可以 比如使用 print(soup.head.contents),就获得了head下的所有子孩子,以列表的形式返回结果,
可以使用 [num] 的形式获得 ,获得标签,使用.name 就可以。
获取标签的孩子,也可以使用children,但是不能print(soup.head.children) 没有返回列表,返回的是 <listiterator object at 0x108e6d150>,
不过使用list可以将其转化为列表。当然可以使用for 语句遍历里面的孩子。
关于string属性,如果超过一个标签的话,那么就会返回None,否则就返回具体的字符串print(soup.title.string) 就返回了 The Dormouse's story
超过一个标签的话,可以试用strings
向上查找可以用parent函数,如果查找所有的,那么可以使用parents函数
查找下一个兄弟使用next_sibling,查找上一个兄弟节点使用previous_sibling,如果是查找所有的,那么在对应的函数后面加s就可以
G. from bs4 import BeautifulSoup在python3用什么替代了
不变,仍用 from bs4 import BeautifulSoup
如果没有安装bs4,需要在命令行工具中使用pip命令进行安装,pip install bs4
H. python2、python3如何导入模块,为什么我在run这个py时候,总是报找不到这个模块
你把bs4换成beautifulsoup试试
I. Python2 和 Python3下安装BeautifulSoup4
Windows键+R 输入cmd
J. python3.5怎么安装beautifulsoup4
For python2.x:
sudo pip install BeautifulSoup4
For python3:
sudo apt-get install python3-bs4