我是Python的新手,实际上我正在研究一个小的Python脚本,该脚本请求并读取URL的HTML。
对于信息,我正在使用的网页是http://bitcoinity.org/markets,我希望使用我的脚本来获取市场的当前价格。我检查了HTML代码,发现Price处于平衡状态:
<span id="last_price" value="447.77"</span>
这是我的Python脚本的代码:
import urllib2
import urllib
from bs4 import BeautifulSoup
url = "http://bitcoinity.org/markets"
values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib2.Request(url, data, headers)
HTML = urllib2.urlopen(req)
soup = BeautifulSoup(HTML)
HTMLText = HTML.read()
HTML.close()
#print soup.prettify()
#print HTMLText
因此,问题在于此脚本的输出(使用2种方法BeautifulSoup和read())如下所示:
</span>
<span id="last_price">
</span>
“ value =”属性丢失,语法更改了,所以我不知道服务器是否不允许我请求该值,或者我的代码是否有问题。
欢迎所有帮助!:)
(对不起我的英语不好,我不是本地人)
价格是通过一组javascript函数计算得出的,在这种情况下,urllib2
+BeautifulSoup
方法无效。
考虑使用利用真实浏览器的工具,例如selenium
:
>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> driver.get('http://bitcoinity.org/markets')
>>> driver.find_element_by_id('last_price').text
u'0.448'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句