Javascript 解析网页时调用onclick方法
免责声明:这是我第一次尝试网络抓取。如果这是一个非常简单的问题,请不要介意 我从一个网站上获取html源代码,如下所示Javascript 解析网页时调用onclick方法,javascript,python,beautifulsoup,urllib2,Javascript,Python,Beautifulsoup,Urllib2,免责声明:这是我第一次尝试网络抓取。如果这是一个非常简单的问题,请不要介意 我从一个网站上获取html源代码,如下所示 def get_soup(site): """get the html source for a web site""" try: logging.debug("Getting data from:" + site) req = urllib2.Request(site) req.add_header('Accept', '*/*') req.a
def get_soup(site):
"""get the html source for a web site"""
try:
logging.debug("Getting data from:" + site)
req = urllib2.Request(site)
req.add_header('Accept', '*/*')
req.add_header('User-Agent', 'web-scraping')
page = urllib2.urlopen(req).read()
return BeautifulSoup(page)
except:
logging.debug("Check the site: " + str(site))
raise
汤有一个如下的onclick方法。在发出url请求时(最好)或之后(如果可能的话),是否有方法调用此方法
soup=get_soup(opt_site.replace(“,symbol).replace(“,expiry.strftime(“%d%b%Y”))
现货价格=汤.findAll('table')[0]。findChildren('td')[1]。findChildren('span')[0]
截至2013年12月23日15:30:25 IST
BeautifulSoup不处理javascript。您应该使用能够处理javascript的东西;例如
soup = get_soup(opt_site.replace("<symbol>", symbol).replace("<expiry>", expiry.strftime("%d%b%Y")))
spot_price = soup.findAll('table')[0].findChildren('td')[1].findChildren('span')[0]
<span>As on Dec 23, 2013 15:30:25 IST<a> <img src="/live_market/resources/images/refressbtn.gif" onclick="refresh();" style="cursor: pointer" title="refresh" /></a></span>