2017-08-16 45 views
4

我知道,這個問題或類似的問題已經被問到。但是我找到的那些人並沒有爲我提供正確的答案,所以我在這裏問。閱讀python中的網頁文字

如何獲取HTML網站的文本,並且可以使用它將其與其他給定值進行比較?

可以說我有這個網頁:

<html> 
<head> 
<title>This is my page</title> 

<center> 
<div class="mon_title">Some title here</div> 
<table class="mon_list" > 
<tr class='list'><th class="list" align="center"></th><th class="list" align="center">Set 1</th><th class="list" align="center">Set 2</th><th class="list" align="center">Set 4</th><th class="list" align="center">Set 5</th><th class="list" align="center">Set 6</th><th class="list" align="center">Set 7</th><th class="list" align="center">Set 8</th><th class="list" align="center">Set 9</th><th class="list" align="center">Set 10</th><th class="list" align="center">Set 11</th><th class="list" align="center">Set 12</th></tr> 
<tr class='list even'><td class="list" align="center">Value 1</td><td class="list" align="center">Value 2</td><td class="list" align="center">Value 3</td><td class="list" align="center">Value 4</td><td class="list" align="center">Value 5</td><td class="list">Value 6</td><td class="list">Value 7</td><td class="list" align="center">Value 8</td><td class="list" align="center">Value 9</td><td class="list" align="center">Value 10</td><td class="list" align="center">Value 11</td><td class="list" align="center">Value 12</td></tr> 
<tr class='list even'><td class="list" align="center">Value 1</td><td class="list" align="center">Value 2</td><td class="list" align="center">Value 3</td><td class="list" align="center">Value 4</td><td class="list" align="center">Value 5</td><td class="list">Value 6</td><td class="list">Value 7</td><td class="list" align="center">Value 8</td><td class="list" align="center">Value 9</td><td class="list" align="center">Value 10</td><td class="list" align="center">Value 11</td><td class="list" align="center">Value 12</td></tr> 
</table> 

對不起,任何拼寫錯誤或缺件。我希望你明白這一點。 現在,我的程序應該讀取,如果某些給定的值與表中給定的值相同,如「值2是否在某處?如果它實際上應該問「是同一行中的值5?」

這通常可能嗎? 構建程序需要多少努力?

我的一切IST下載實際的全功能HTML網頁使用此代碼在python:

import requests 

url = 'http://some.random.site.com/you/ad/here' 
print (requests.get(url).text) 

這給了我你在上面看到的HTML代碼。相反,我希望你在網站上點擊CTRL + A時獲得的內容,並複製並粘貼到編輯器文件中。 PS:我是相當新的編程,所以很抱歉,如果有任何概念,我真的不知道或喜歡它。 此外,對不起,我的英語我是德國人......

+0

您應該使用HTML解析庫像BS4或LXML –

回答

2

您可以使用urllibre找到值:

import urllib.request 
import re 

data = str(urllib.request.urlopen(url).read()) 

values = re.findall("Value \d+", data) 

輸出:

['Value 1', 'Value 2', 'Value 3', 'Value 4', 'Value 5', 'Value 6', 'Value 7', 'Value 8', 'Value 9', 'Value 10', 'Value 11', 'Value 12', 'Value 1', 'Value 2', 'Value 3', 'Value 4', 'Value 5', 'Value 6', 'Value 7', 'Value 8', 'Value 9', 'Value 10', 'Value 11', 'Value 12'] 
+0

我的輸出:數據= STR(了urllib.urlopen(URL).read()) AttributeError的: '模塊' 對象有 '的urlopen' –

+1

@LeoLion固定無屬性。請參閱我最近的編輯。 – Ajax1234

+0

它實際上做了伎倆。謝謝您的幫助。儘管它對我的項目來說並不是100%完美,但它肯定會起作用。 –

1
import requests 
from bs4 import BeautifulSoup as soup 
url = 'http://some.random.site.com/you/ad/here' 
text=soup(requests.get(url).text) 
text=text.find(class_='mon_list') 
listy=[] 
rows = table.findAll('tr') 
for tr in rows: 
    cols = tr.findAll('td') 
    listy.append([elem.get_text() for elem in cols]) 
print(listy) 

這將給它一個嵌套列表:

[[], ['Value 1', 'Value 2', 'Value 3', 'Value 4', 'Value 5', 'Value 6', 'Value 7', 'Value 8', 'Value 9', 'Value 10', 'Value 11', 'Value 12'], ['Value 1', 'Value 2', 'Value 3', 'Value 4', 'Value 5', 'Value 6', 'Value 7', 'Value 8', 'Value 9', 'Value 10', 'Value 11', 'Value 12']]