Fetch data of variables inside script tag in Python or Content added from js

后端 未结 2 798
礼貌的吻别
礼貌的吻别 2020-12-14 11:23

I want to fetch data from another url for which I am using urllib and Beautiful Soup , My data is inside table tag (which I have figure out using Firefox co

2条回答
  •  太阳男子
    2020-12-14 11:46

    Just to add to @mhawke 's answer, rather than hardcoding the offset of the script tag, you loop through all the script tags and match the one that matches your pattern;

    web = urllib.urlopen("http://www.nasdaq.com/quotes/nasdaq-financial-100-stocks.aspx")
    pattern = re.compile('var table_body = (.*?);')
    
    soup = BeautifulSoup(web.read(), "lxml")
    scripts = soup.find_all('script')
    for script in scripts:
       if(pattern.match(str(script.string))):
           data = pattern.match(script.string)
           stock = json.loads(data.groups()[0])
           print stock
    

提交回复
热议问题