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