2016-03-11 50 views
0

讀取特定類的網頁我有使用HTMLParser一個腳本,從網頁讀取data使用python

import urllib 
from HTMLParser import HTMLParser 
import re 


class get_HTML_Info(HTMLParser): 
    def handle_data(self, data): 
     print data 


adib = urllib.urlopen('http://www.bulldoghax.com/secret/spinner') 
htmlsource = adib.read() 
adib.close() 

parser = get_HTML_Info() 
parser.feed(str(htmlsource)) 

我結束了兩個組數據是這樣的:

bulldoghax 

8530330882 

在終端中,我只想提取只有該數字並將其設置爲python中的字符串。

回答

1

使用美麗的湯來刮取數據。

pip install BeautifulSoup

import urllib 
from HTMLParser import HTMLParser 
import re 

adib = urllib.urlopen('http://www.bulldoghax.com/secret/spinner') 

htmlsource = adib.read() 

from bs4 import BeautifulSoup 
soup = BeautifulSoup(htmlsource) 
for each_div in soup.findAll('div',{'class':'number'}): 
    print each_div.text 
+0

謝謝!,這是完美的!,我只是不得不改變'湯= BeautifulSoup(htmlsource)''湯= BeautifulSoup(htmlsource, 「lxml」)'因爲它在第一次嘗試時給了我一個錯誤 – shoomy

+0

@himanshu_dua你能幫我編寫一個代碼,它發送一個cookie數值給這個網站'http://www.bulldoghax.com/secret/codes' – shoomy

1

簡單,在這裏:

n="".join(filter(str.isdigit, data)) 

它過濾基礎上是一個數字或不是字符串,然後加入它爲一個字符串。

+0

謝謝你,現在它只是顯示號碼,反正有我可以刪除「\ n」新行的事,我只是想輸出是數字 – shoomy

+0

@shoomy它應該刪除換行符,看看這個:https://repl.it/BvMq/1 – Maltysen