从用python阅读的网页中读取某些内容
我正在尝试从一个web上的python模块读取一些数据 我设法阅读,但是在解析这些数据和获取所需信息方面有一些困难 我的代码如下。感谢您的帮助从用python阅读的网页中读取某些内容,python,Python,我正在尝试从一个web上的python模块读取一些数据 我设法阅读,但是在解析这些数据和获取所需信息方面有一些困难 我的代码如下。感谢您的帮助 #!/usr/bin/python2.7 -tt import urllib import urllib2 def Connect2Web(): aResp = urllib2.urlopen("https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices
#!/usr/bin/python2.7 -tt
import urllib
import urllib2
def Connect2Web():
aResp = urllib2.urlopen("https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices.jsp");
web_pg = aResp.read();
print web_pg
#Define a main() function that prints a litte greeting
def main():
Connect2Web()
# This is the standard boilerplate that calls the maun function.
if __name__ == '__main__':
main()
当我打印这个时,我会打印整个网页
我想从中提取一些信息(例如,
“银存折帐户”
并从中获取利率),我在解析此html文档时遇到一些困难。可以使用regexps获取所需数据:
import urllib
import urllib2
import re
def Connect2Web():
aResp = urllib2.urlopen("https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices.jsp");
web_pg = aResp.read();
pattern = "<td><b>SILVER PASSBOOK ACCOUNT</b></td>" + "<td>(.*)</td>" * 4
m = re.search(pattern, web_pg)
if m:
print "SILVER PASSBOOK ACCOUNT:"
print "\tCurrency:", m.group(1)
print "\tUnit:", m.group(2)
print "\tBank Sells:", m.group(3)
print "\tBank Buys:", m.group(4)
else:
print "Nothing found"
导入urllib
导入urllib2
进口稀土
def Connect2Web():
aResp=urllib2.urlopen(“https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices.jsp");
web_pg=arsp.read();
pattern=“银存折账户”+”(.*)*4
m=重新搜索(模式、网页)
如果m:
打印“银存折帐户:”
打印“\t电流:”,m.group(1)
打印“\t:”,m.group(2)
打印“\t银行销售:”,m.group(3)
打印“\t银行购买:”,m.group(4)
其他:
打印“未找到任何内容”
如果在循环中进行匹配,请不要忘记重新编译模式。不建议使用re来匹配XML/HTML。然而,它有时也能起作用。最好使用HTML解析器和DOM API。下面是一个例子:
import html5lib
import urllib2
aResp = urllib2.urlopen("https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices.jsp")
t = aResp.read()
dom = html5lib.parse(t, treebuilder="dom")
trlist = dom.getElementsByTagName("tr")
print trlist[-3].childNodes[1].firstChild.childNodes[0].nodeValue
您可以迭代trlist
以查找您感兴趣的数据
从注释中添加:html5lib
是第三方模块。看见easy\u install
或pip
程序应该可以安装它。您也可以尝试。
和/或可以使用XPath(带/不带抓取)。可能以后对您有用,这里有一些示例:
g = Grab()
g.go(address)
user_div = g.xpath('//*/div[@class="user_profile"]') # main <div> for parse
country = user_div.find('*/*/a[@class="country-name"]')
region = user_div.find('*/*/a[@class="region"]') # look for <a class="region">
city = user_div.find('*/*/a[@class="city"]')
friends = [ i.text_content() for i in user_div.findall('dl[@class="friends_list"]/dd/ul/li/a[@rel="friend"]') ]
# and another ability, i.e. you have 2 tags:
# <tr> <td>Text to grab</td> <td>if only that tag contains this text</td> </tr>
val = user_div.xpath(u"dl/dt[contains(text(),'%s')]/../dd/text()" % 'if only that tag contains this text')
# print val[0] <- will contain 'Text to grab'
g=Grab()
g、 go(地址)
user_div=g.xpath('/*/div[@class=“user_profile”]')#用于解析的main
country=user_div.find('*/*/a[@class=“country name”]”)
region=user_div.find('*/*/a[@class=“region”]')#查找
city=user_div.find('*/*/a[@class=“city”]”)
friends=[i.text\u content(),用于用户_div.findall中的i('dl[@class=“friends\u list”]/dd/ul/li/a[@rel=“friend”]')]
#还有另一种能力,即你有两个标签:
#如果只有该标记包含此文本,则要抓取的文本
val=user_div.xpath(u“dl/dt[contains(text(),'%s')]/../dd/text()“%”,如果只有该标记包含此文本')
#打印val[0]您到底遇到了什么困难?我尝试使用“findall”方法,但不确定应该使用哪些参数???re.findall(pattern,string[,flags])意思是pattern=[“SILVER PASSBOOK ACCOUNT”]string=web\u pgit无法说出。。。回溯(最后一次调用):文件“c:\Work\Learn\Python\Web.py”,第27行,在main()文件“c:\Work\Learn\Python\Web.py”,第23行,在main Connect2Web()文件“c:\Work\Learn\Python\Web.py”,第18行,在Connect2Web matches=re.findall([“SILVER”],Web\u pg)文件“c:\Program Files\Python27\lib\re.py”,第177行,在findall return\u compile(pattern,flags).findall(string)文件“c:\Program Files\Python27\lib\re.py”第231行中,在\u compile p=\u cache.get(cachekey)TypeError:unhable type:“list”请回答问题,并在那里添加信息(如果可能的话,格式良好)。谢谢Keith,但我无法编译上述内容,我使用的是Python2.7.2,看起来无法获得“html5lib”模块。请问您使用的是哪一版本的Python???@tush1r您需要安装它。确保您有easy\u安装
。如果没有,谷歌ez_安装程序。哦,我忘了那是第三方模块。看见easy\u install
或pip
程序应该能够安装它。在d.getElementsByTagName(“tr”)-“d”未定义中似乎键入了一些错误。也许他指的是“dom”,而不是“d”?