requests.get()Python 2中缺少数据

时间:2018-05-31 16:25:17

标签: python beautifulsoup python-requests

我想在https://www.settrade.com/AnalystConsensus/C04_10_stock_saa_p1.jsp?txtSymbol=PTT&ssoPageId=9&selectPage=10

上截取IAA共识价格

在Google Chrome检查元素中,我可以通过beautifulsoup使用<h3>来获取数据。但是从打印page.content我得到了

...
<h3 class="colorGreen"></h3>
...

它应该是<h3 class="colorGreen">62.00</h3>

这是我的代码

import requests
from bs4 import BeautifulSoup

def findPrice(Quote):
    link = "http://www.settrade.com/AnalystConsensus/C04_10_stock_saa_p1.jsp?txtSymbol="+Quote+"&ssoPageId=9&selectPage=10"
    page = requests.get(link)
    soup = BeautifulSoup(page.content,'html.parser')

    print page.content
    target = soup.findAll('h3')
    return target.string

findPrice('PTT')

1 个答案:

答案 0 :(得分:0)

我猜,服务器正在检查LstQtLst Cookie,并生成填写了“共识目标价格”的HTML。

import requests
from bs4 import BeautifulSoup


def find_price(quote):
    link = ('http://www.settrade.com/AnalystConsensus/C04_10_stock_saa_p1.jsp'
            '?txtSymbol={}'
            '&ssoPageId=9'
            '&selectPage=10'.format(quote))

    html = requests.get(link, cookies={'LstQtLst': quote}).text
    soup = BeautifulSoup(html, 'html.parser')

    price = soup.find('h3').string
    return price
>>> find_price('PTT')
62.00