beautifulsouppython
1. python中beautifulsoup簡單使用
file = open(filename)
soup=Beautifulsoup(file,'lxml' )
aElement = soup.select('div#autInfo div.info_c a')
text1 = aElement.string
text2 = soup.select('div#autDescription div.info_c').string
我是按你圖片的內容取的,select裡面遵循CSS選擇器語法,具體的CSS選擇器語法你可以網路一下。
2. 如何在 python 中使用 beautifulsoup4 來抓取標簽中的內容
小白入門階段,主要用requests和beautifulsoup4庫來爬取內容。目前遇到的問題是,使用beautifulsoup抓取標簽內容出錯。所以來咨詢下過往前輩的建議。
1、像上圖HTML文檔中的滴滴出行,應該如何抓取?用select函數可以實現嘛?
2、像抓取戰略投資,我使用了下面的語句,內容截取到了,但是還多了個括弧。不知道怎麼把括弧去掉。
investment=soup.select('span[class="t-small c-green"]')[0].text.strip()
3、我光是select函數就用迷糊了。。。更別說添加別的函數了。
問題比較簡單,但是已經卡了我很久了。求大神指點一二啊!
from bs4 import BeautifulSoup
html_doc = '''
<div class="line-title">
<span class="title">
<b>
滴滴出行
<span class="t-small c-green">
(戰略投資)
</span>
</b>
</span>
<span class="sechovershow jzbtn c-lined small marl10 act-ugc-edit act-ugc-edit-base1" style="margin-top:-5px">
<i class="fa fa-pencil"></i>
編輯
</span>
</div>
'''
soup = BeautifulSoup(html_doc, "html.parser")
# 初級版
didi = soup.b.next_element.strip()
invest = soup.b.span.next_element.strip()
# 進階版
didi, invest = soup.b.stripped_strings
3. python中的beautifulsoup和xpath有什麼異同點
BeautifulSoup是一個庫,而XPath是一種技術,python中最常用的XPath庫是lxml,因此,這里就拿lxml來和BeautifulSoup做比較吧
1 性能 lxml >> BeautifulSoup
BeautifulSoup和lxml的原理不一樣,BeautifulSoup是基於DOM的,會載入整個文檔,解析整個DOM樹,因此時間和內存開銷都會大很多。而lxml只會局部遍歷,另外lxml是用c寫的,而BeautifulSoup是用python寫的,因此性能方面自然會差很多。
2 易用性 BeautifulSoup >> lxml
BeautifulSoup用起來比較簡單,API非常人性化,支持css選擇器。lxml的XPath寫起來麻煩,開發效率不如BeautifulSoup。
title = soup.select('.content div.title h3')
同樣的代碼用Xpath寫起來會很麻煩
title = tree.xpath("//*[@class='content']/div[@class='content']/h3")
3 總結
需求比較確定,要求性能的場合用lxml,快速開發用BeautifulSoup
ps: BeautifulSoup4可以使用lxml作為parser了
4. Python beautifulsoup 獲取標簽中的值 怎麼獲取
age = soup.find(attrs={"class":"age"}) #你這里find只要一個attrs參數不會報錯。
if age == None: #簡單點可以用 if not age:
print u'沒有找到'
else:
soup.find(attrs={"class":"name"})
#否則用findAll找出所有具有這個class的tr
tr = html.find("tr", attrs={"class":"show_name"})
tds = tr.findAll("td")
for td in tds:
print td.string # 或許不是string屬性,你可以用dir(td)看看有哪些可用的。
(4)beautifulsouppython擴展閱讀:
1、如果是函數定義中參數前的*表示的是將調用時的多個參數放入元組中,**則表示將調用函數時的關鍵字參數放入一個字典中。
1)如定義以下函數:
def func(*args):print(args)
當用func(1,2,3)調用函數時,參數args就是元組(1,2,3)
2)如定義以下函數:
def func(**args):print(args)
當用func(a=1,b=2)調用函數時,參數args將會是字典{'a':1,'b':2}
學python的同時一定會接觸到其他技術,畢竟光會python這門語言是不夠的,要看用它來做什麼。比如說用 python做爬蟲,就必須接觸到html, http等知識。
python是現在最火的數據分析工具語言python的進階的路線是數據清洗,爬蟲,數據容器,之後是卷積,線性分析,和機器學習,區塊連,金融方面的量化等高端進階。
5. python beautifulsoup應用問題
最好是先看一下 driver.page_source 的內容,確保裡面有你先要的數據。
查一下 beautifulsoup 的文檔,有關 css 選擇器的部分,對一下 find_all 裡面的語法是不是正確 。在寫 find_all 的時候一層一層選下去,不要一開始就寫到最裡面一層。
平時寫解析的時候用 lxml 比較多,beautifulsoup 的寫法太久沒寫過了。
6. python怎麼安裝beautifulsoup
在cmd(命令行)中輸入以下命令:
python -m pip install bs4從bs4中調用beautifulsoup:
from bs4 import BeautifulSoup
7. mac版python怎麼安裝beautifulsoup
一、安裝python
sudo rm -rf /System/Library/Frameworks/Python.framework/
sudo rm -rf /Library/Frameworks/Python.framework/
sudo rm -rf /Applications/Python\ 2.7/
sudo rm -rf /usr/local/bin/*
重啟機器
以上步驟是卸載已安裝的python,第1個命令卸載系統自帶的,第2個命令卸載用戶安裝的。
二、安裝pydev(Eclipse中的python插件)
在pydev官網下載pydev的zip包,這比傳統的使用site.xml的eclipse插件安裝方式效率要高。
對於eclipse3.4以上版本,將zip包解壓到eclipse/dropin即可。
三、安裝beautifulsoup4
解壓安裝包,shell下先cd進入目錄,然後python setup.py install。
四、安裝lxml
lxml是強大的xml和html的python庫。
五、安裝crapy
首先安裝setuptools, 選擇與本機python版本一致的安裝包,如setuptools-0.6c11-py2.7.egg (md5)
shell下sh setuptools-0.6c11-py2.7.egg。
setuptools安裝後可使用easy_install命令,這是安裝python相關包的命令。
8. python beautifulsoup可以做什麼
Beautiful Soup是用Python寫的一個HTML/XML的解析器,它可以很好的處理不規范標記並生成剖析樹(parse tree)。
它提供簡單又常用的導航(navigating),搜索以及修改剖析樹的操作。
它可以大大節省你的編程時間。
9. Python關於BeautifulSoup的用法
創建一個字元串,例子如下:
Python
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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>
創建 beautifulsoup 對象
Python
soup = BeautifulSoup(html)
另外,還可以用本地 HTML 文件來創建對象,例如
soup = BeautifulSoup(open('index.html'))
上面這句代碼便是將本地 index.html 文件打開,用它來創建 soup 對象
下面來列印一下 soup 對象的內容,格式化輸出
print soup.prettify()
<html>
<head>
<title>
The Dormouse's story
</title>