繁体   English   中英

在Raspberry Pi中读取URL

[英]Read a URL in Raspberry Pi

我想读取URL中存在的数据。 例如,如果我有以下URL:

http://robolab.in/home-automation.html#ON

我想读取状态“ ON”,而保留其余URL。 如何才能做到这一点?

您尝试做的事情称为网页抓取。 在使用urllib / urllib2库的python中,您可以实现此目标。

import urllib

try:
    html=urllib.urlopen('http://robolab.in/home-automation.html#ON')
    htmltext=html.read()
except:
    print 'error opening link'

print htmltext

这会打印您的浏览器显示的html文本。 现在这只是一个字符串...您可以随时对其进行操作。 但是,如果您安装了BeautifulSoup,则可以编写如下代码:

from bs4 import BeautifulSoup

soup=BeautifulSoup(htmltext)
for script in soup(["script", "style"]):
    script.extract()
text = soup.get_text()
print text

使用此代码并给出您的网址,我得到了:

Robolab Technologies
Home Automation

OFF

您可以轻松进行

status=''
text=text.strip()
for index,line in enumerate(text):
    if index>3:
        status = line
if 'ON' in status:
    print "it's on"
else:
    print "it's off"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM