BeautifulSoup不会返回任何价值

时间:2017-02-10 07:21:54

标签: python-2.7 web-scraping beautifulsoup

我是Beautifulsoup的新手,似乎遇到了问题。我写的代码是正确的,但输出是空的。它没有显示任何价值。

import requests
from bs4 import BeautifulSoup

url = requests.get("https://www.nseindia.com/")

soup = BeautifulSoup(url.content, "html.parser")

nifty = soup.find_all("span", {"id": "lastPriceNIFTY"})

for x in nifty:
    print x.text

1 个答案:

答案 0 :(得分:1)

该页面似乎是由javascript呈现的。 import dryscrape from bs4 import BeautifulSoup sess = dryscrape.Session() sess.visit("https://www.nseindia.com/") soup = BeautifulSoup(sess.body(), "lxml") nifty = soup.select("span[id^=lastPriceNIFTY]") print nifty[0:2] #printing sample i.e first two entries. 将无法获取JavaScript加载的内容,它将在JavaScript呈现之前获取部分页面。您可以像这样使用dryscrape库:

[<span class="number" id="lastPriceNIFTY 50"><span class="change green">8,792.80 </span></span>, <span class="value" id="lastPriceNIFTY 50 Pre Open" style="color:#000000"><span class="change green">8,812.35 </span></span>]

输出:

{{1}}