使用beautifulsoup在moneycontrol网站上的搜索框中填充股票输入 [英] Fill stock input in searchbox on moneycontrol site using beautifulsoup

查看:93
本文介绍了使用beautifulsoup在moneycontrol网站上的搜索框中填充股票输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Moneycontrol site获取stock信息.我要在searchbox中填写股票名称. html代码:

<input class="txtsrchbox FL" id="search_str" 
      onkeyup="getAutosuggesionHeader('#form_topsearch');" 
      onclick="getAutosuggesionHeader('#form_topsearch');" 
      placeholder="Search Quotes, News, NAVs" name="search_str" 
      value="" type="text" autocomplete="off">

如何使用beautifulsoup实现此目标.

我尝试了以下代码:

from BeautifulSoup import BeautifulSoup

import urllib

post_params = {'value' : 'SBIN' }

post_args = urllib.parse.urlencode(post_params).encode("utf-8")

url = 'https://www.moneycontrol.com'

fp = urllib.request.urlopen(url, post_args)

soup = BeautifulSoup(fp)

解决方案

我不熟悉财务信息,因此这可能没有用,但也许是有用的,或者QHarr可以解决它.看起来它为(t:time ?, cp:收盘价?v:成交量?ap:???)返还了一些价值

所以我注意到您可以从图表数据中得到一些响应,但是它需要输入一个参数以进行查询(例如,对于SBIN,sc_id值是SBI.)

因此,我需要一种方法来获取该sc_id值,并看到它们具有建议的"文本搜索返回值,可以访问该文本返回值以输入您的搜索词(即:"sbin"),以获取该sc_id./p>

因此,这里有一个小脚本,可以为SBIN找回一些信息.希望这可以从中得到一些利用:

import requests
import json

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'}


# Enter search text
query_input = input('Enter Search Text: ')

#Get suggested sc_id
suggest_query_url = 'https://www.moneycontrol.com/mccode/common/autosuggestion_solr.php'

query = {
'classic': 'true',
'query': query_input,
'type': '1',
'format': 'json',
'callback': 'suggest1'}


# Pull out the sc_id
suggested_response = requests.get(suggest_query_url , headers=headers, params=query).text
suggested_response = suggested_response.split('(',1)[1]
suggested_response = suggested_response.rsplit(')',1)[0]

sc_id = json.loads(suggested_response)[0]['sc_id']


# Use the sc_id to get BSE and NSE data
request_url = 'https://www.moneycontrol.com/stocks/company_info/get_vwap_chart_data.php'
query = {'sc_did': sc_id}

response = requests.get(request_url, headers=headers, params=query).json()

输出:

print (response)
{'BSE': [{'t': '1551949665', 'ap': '279.70', 'cp': '279.70', 'v': '2151'}, {'t': '1551949704', 'ap': '279.70', 'cp': '279.70', 'v': '2151'}, {'t': '1551949740', 'ap': '279.70', 'cp': '279.70', 'v': '2151'}, {'t': '1551950159', 'ap': '278.93', 'cp': '278.90', 'v': '18755'}, {'t': '1551950219', 'ap': '278.90', 'cp': '278.70', 'v': '23368'}, {'t': '1551950279', 'ap': '278.89', 'cp': '279.00', 'v': '32498'}, {'t': '1551950338', 'ap': '278.91', 'cp': '279.00', 'v': '36396'}, {'t': '1551950399', 'ap': '278.90', 'cp': '278.80', 'v': '42964'}, {'t': '1551950459', 'ap': '278.88', 'cp': '278.35', 'v': '45685'}, {'t': '1551950519', 'ap': '278.76', 'cp': '278.30', 'v': '54082'}, {'t': '1551950579', 'ap': '278.74', 'cp': '278.30', 'v': '56780'}, {'t': '1551950639', 'ap': '278.69', 'cp': '278.20', 'v': '62504'}, {'t': '1551950699', 'ap': '278.68', 'cp': '278.10', 'v': '63338'}, {'t': '1551950759', 'ap': '278.68', 'cp': '278.10', 'v': '63723'}, {'t': '1551950819', 'ap': '278.67', 'cp': '277.80', 'v': '64998'}, {'t': '1551950879', 'ap': '278.63', 'cp': '278.20', 'v': '68780'}, {'t': '1551950939', 'ap': '278.59', 'cp': '278.60', 'v': '77680'}, {'t': '1551950999', 'ap': '278.59', 'cp': '278.35', 'v': '79316'}, {'t': '1551951059', 'ap': '278.58', 'cp': '278.30', 'v': '80566'}, {'t': '1551951119', 'ap': '278.58', 'cp': '278.20', 'v': '81247'}, {'t': '1551951178', 'ap': '278.57', 'cp': '278.10', 'v': '82067'}, {'t': '1551951238', 'ap': '278.57', 'cp': '278.15', 'v': '82918'}, {'t': '1551951294', 'ap': '278.56', 'cp': '278.60', 'v': '85734'}, {'t': '1551951358', 'ap': '278.56', 'cp': '278.40', 'v': '87239'}, {'t': '1551951419', 'ap': '278.56', 'cp': '278.25', 'v': '88039'}, ...

I want to fetch stock information from Moneycontrol site. I want to fill stock name in searchbox. html code:

<input class="txtsrchbox FL" id="search_str" 
      onkeyup="getAutosuggesionHeader('#form_topsearch');" 
      onclick="getAutosuggesionHeader('#form_topsearch');" 
      placeholder="Search Quotes, News, NAVs" name="search_str" 
      value="" type="text" autocomplete="off">

How can i achieve this using beautifulsoup.

I have tried following code:

from BeautifulSoup import BeautifulSoup

import urllib

post_params = {'value' : 'SBIN' }

post_args = urllib.parse.urlencode(post_params).encode("utf-8")

url = 'https://www.moneycontrol.com'

fp = urllib.request.urlopen(url, post_args)

soup = BeautifulSoup(fp)

解决方案

I'm not familiar with financial info so this might not be of use, but maybe it is, or maybe QHarr can work off of it. Looks like it gives back some value for (t:time?, cp:closing price? v:volume? ap:???)

So I noticed you can get some response from the chart data, however it requires to enter a parameter to query (ie. a sc_id value, in this case for SBIN, is SBI.)

So I needed a way though to get that sc_id value, and saw they have a "suggested" text search return that can be accessed to enter your search word (ie: "sbin"), to get back that sc_id.

So here's a little script that was able to get some info back for SBIN. Hopefully this can get some use out of it:

import requests
import json

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'}


# Enter search text
query_input = input('Enter Search Text: ')

#Get suggested sc_id
suggest_query_url = 'https://www.moneycontrol.com/mccode/common/autosuggestion_solr.php'

query = {
'classic': 'true',
'query': query_input,
'type': '1',
'format': 'json',
'callback': 'suggest1'}


# Pull out the sc_id
suggested_response = requests.get(suggest_query_url , headers=headers, params=query).text
suggested_response = suggested_response.split('(',1)[1]
suggested_response = suggested_response.rsplit(')',1)[0]

sc_id = json.loads(suggested_response)[0]['sc_id']


# Use the sc_id to get BSE and NSE data
request_url = 'https://www.moneycontrol.com/stocks/company_info/get_vwap_chart_data.php'
query = {'sc_did': sc_id}

response = requests.get(request_url, headers=headers, params=query).json()

Output:

print (response)
{'BSE': [{'t': '1551949665', 'ap': '279.70', 'cp': '279.70', 'v': '2151'}, {'t': '1551949704', 'ap': '279.70', 'cp': '279.70', 'v': '2151'}, {'t': '1551949740', 'ap': '279.70', 'cp': '279.70', 'v': '2151'}, {'t': '1551950159', 'ap': '278.93', 'cp': '278.90', 'v': '18755'}, {'t': '1551950219', 'ap': '278.90', 'cp': '278.70', 'v': '23368'}, {'t': '1551950279', 'ap': '278.89', 'cp': '279.00', 'v': '32498'}, {'t': '1551950338', 'ap': '278.91', 'cp': '279.00', 'v': '36396'}, {'t': '1551950399', 'ap': '278.90', 'cp': '278.80', 'v': '42964'}, {'t': '1551950459', 'ap': '278.88', 'cp': '278.35', 'v': '45685'}, {'t': '1551950519', 'ap': '278.76', 'cp': '278.30', 'v': '54082'}, {'t': '1551950579', 'ap': '278.74', 'cp': '278.30', 'v': '56780'}, {'t': '1551950639', 'ap': '278.69', 'cp': '278.20', 'v': '62504'}, {'t': '1551950699', 'ap': '278.68', 'cp': '278.10', 'v': '63338'}, {'t': '1551950759', 'ap': '278.68', 'cp': '278.10', 'v': '63723'}, {'t': '1551950819', 'ap': '278.67', 'cp': '277.80', 'v': '64998'}, {'t': '1551950879', 'ap': '278.63', 'cp': '278.20', 'v': '68780'}, {'t': '1551950939', 'ap': '278.59', 'cp': '278.60', 'v': '77680'}, {'t': '1551950999', 'ap': '278.59', 'cp': '278.35', 'v': '79316'}, {'t': '1551951059', 'ap': '278.58', 'cp': '278.30', 'v': '80566'}, {'t': '1551951119', 'ap': '278.58', 'cp': '278.20', 'v': '81247'}, {'t': '1551951178', 'ap': '278.57', 'cp': '278.10', 'v': '82067'}, {'t': '1551951238', 'ap': '278.57', 'cp': '278.15', 'v': '82918'}, {'t': '1551951294', 'ap': '278.56', 'cp': '278.60', 'v': '85734'}, {'t': '1551951358', 'ap': '278.56', 'cp': '278.40', 'v': '87239'}, {'t': '1551951419', 'ap': '278.56', 'cp': '278.25', 'v': '88039'}, ...

这篇关于使用beautifulsoup在moneycontrol网站上的搜索框中填充股票输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆