在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/數(shù)據(jù)分析&挖掘  Python/ 【求教】:如何用python爬取網(wǎng)絡(luò)中tr,td以下的內(nèi)容

【求教】:如何用python爬取網(wǎng)絡(luò)中tr,td以下的內(nèi)容

請教各位大神,我要怎么爬出這個網(wǎng)頁html中灰色部分最后的2.1610這個數(shù)字?
圖片描述

并且我有一系列和這個網(wǎng)頁具有高度相似html的網(wǎng)頁,我想要爬出同樣位置的這一串?dāng)?shù)字,我應(yīng)該如何利用beautifulsoup完成我的代碼?

現(xiàn)我的代碼如下(注釋部分采用了第一位回答者的代碼):

def getLinks(articleUrl):
    html=urlopen(articleUrl)
    #s = '<tr><td><b><a href=".././statistics/power" title="Exponent of the power-law degree distibution">Power law exponent (estimated) with d<sub>min</sub></a></b></td><td>2.1610(d<sub>min</sub> = 2) </td></tr>'
    #soup = BeautifulSoup(s, 'html.parser')
    #print(soup.find_all('td')[1].contents[0][:-2])
回答
編輯回答
喵小咪

Python 的網(wǎng)頁解析一般有以下方法:
1.字符串方法
2.正則表達(dá)式
3.html/xml文本解析庫的調(diào)用(如著名的BeautifulSoup庫)
對于你所給的例子, 假設(shè):

>>> s = '<tr><td><b><a href=".././statistics/power" title="Exponent of the power-law degree distibution">Power law exponent (estimated) with d<sub>min</sub></a></b></td><td>2.1610(d<sub>min</sub> = 2) </td></tr>'

由于文本特征非常明顯, 可以這樣處理:
1.字符串處理方法:

>>> s.split('<td>')[-1].split('(d')[0]
'2.1610'

2.re:

>>> import re
>>> pattern = re.compile('</b></td><td>(.*)\(d<sub>')
>>> pattern.findall(s)
['2.1610']

3.BeautifulSoup:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(s, 'html.parser')
>>> soup.find_all('td')[1].contents[0][:-2]
'2.1610'

以上方法均是根據(jù)給定的例子臨時設(shè)計的.

2017年1月22日 12:56