【问题标题】:web scraping of webpage on chartink.comchartink.com 上网页的网页抓取
【发布时间】:2021-09-12 06:11:24
【问题描述】:

请帮助我抓取此链接。 链接-https://chartink.com/screener/time-pass-48 我正在尝试进行网络抓取,但它没有显示我想要的表格。请帮助我。

我已经尝试过这段代码,但它没有给我想要的结果。

import requests
from bs4 import BeautifulSoup

URL = 'https://chartink.com/screener/time-pass-48'
page = requests.get(URL)
print(page)

soup = BeautifulSoup(page.content, 'html.parser')
print(soup)

【问题讨论】:

  • 看起来您可能需要使用 Selenium 来抓取该链接。
  • 先生,以 JSON 格式访问或读取 Chartlink 响应是否合法?

标签: python html web web-scraping beautifulsoup


【解决方案1】:

数据确实来自 POST 请求。您不需要允许 JavaScript 运行。您只需要获取一个 cookie(ci_session - 可以使用 Session 对象来保存来自初始登录页面请求的 cookie 以通过后续 POST 传递)和一个令牌(X-CSRF-TOKEN - 可以从meta 初始请求响应中的标签):

import requests
from bs4 import BeautifulSoup as bs
import pandas as pd

data = {
  'scan_clause': '( {cash} ( monthly rsi( 14 ) > 60 and weekly rsi( 14 ) > 60 and latest rsi( 14 ) > 60 and 1 day ago  rsi( 14 ) <= 60 and latest volume > 100000 ) ) '
}

with requests.Session() as s:
    r = s.get('https://chartink.com/screener/time-pass-48')
    soup = bs(r.content, 'lxml')
    s.headers['X-CSRF-TOKEN'] = soup.select_one('[name=csrf-token]')['content']
    r = s.post('https://chartink.com/screener/process', data=data).json()
    #print(r.json())
    df = pd.DataFrame(r['data'])
    print(df)

【讨论】:

  • 谢谢您,先生,非常感谢您,但请告诉我一件事,您的代码显示了 33 个额外的股票,这些股票不在筛选器中,为什么?
  • 网站上的结果是否超过 1 页?
  • 是的,先生,但我想要所有页面。除了你的代码每天额外显示 1-2 只股票。在之前的评论中,这是一个错字,显示了 3 个额外的股票。不是 33 岁。
  • 先生,以 JSON 格式访问或读取 Chartlink 响应是否合法?
  • 从 csv 传递 data 变量以获取不同的扫描仪结果的任何方式
【解决方案2】:
import requests
import bs4
page = requests.get("https://chartink.com/screener/time-pass-48")
bs4.BeautifulSoup(page.text,'lxml')

我认为应该这样做。

【讨论】:

  • 先生,我得到了与我的代码相同的 html 页面,但不是所需的表格。
  • 先生,以 JSON 格式访问或读取 Chartlink 响应是否合法?
【解决方案3】:

您可以通过发出post 请求来访问表数据。您可以查看 Chrome 开发工具网络选项卡,看看哪些元素是从其他地方加载的。

表中的数据是从https://chartink.com/screener/process 发布请求加载的(查看网络选项卡中的“进程”名称)。您可以使用requests 库作为QHarr 建议的post request


或者,您可以通过使用 requests-html 库来实现这一点而不会使事情变得复杂,即使直接从源获取数据会更快,例如发出post 请求。

from requests_html import HTMLSession

session = HTMLSession()
response = session.get('https://chartink.com/screener/time-pass-48')
# renders javascript
response.html.render()

for result in response.html.xpath('//*[@id="DataTables_Table_0"]/tbody/tr'):
    print(f'{result.text}\n')

# part of the output:
'''
1
Kothari Products Limited
KOTHARIPRO
P&F | F.A
19.96%
106.7
262,997
'''

从那里所有需要做的就是split()元素并获得所需的元素(index),例如:

for result in response.html.xpath('//*[@id="DataTables_Table_0"]/tbody/tr'):
    # getting text data, splitting by a new line and grabbing first index [1]
    # the process is the same for every other column
    stock_name = result.text.split('\n')[1]
    print(stock_name)

# part of the output:
'''
Kothari Products Limited
STEELXIND
Oswal Chemicals & Fertilizers Limited
Hbl Power Systems Limited
'''

【讨论】:

  • 回溯(最近一次通话最后):文件“C:/Users/Ayush/AppData/Roaming/JetBrains/PyCharmCE2020.3/scratches/scratch_3.py”,第 6 行,在 响应中.html.render() 文件“C:\Users\Ayush\PycharmProjects\pythonProject\venv\lib\site-packages\requests_html.py”,第 598 行,在渲染内容、结果、页面 = self.session.loop.run_until_complete (self._async_render(url=self.url, script=script, sleep=sleep, wait=wait, content=self.html, reload=reload, scrolldown=scrolldown, timeout=timeout, keep_page=keep_page))
  • response.html.render 出现错误
  • @ayush5624 看起来像 pyppeteer wasn't installed properly。第一次运行 render() 方法时,它会将 Chromium 下载到您的目录中。
  • 那么先生,我应该怎么做才能重新安装它?
  • @ayush5624 尝试卸载pyppeteer 并通过pip install pyppeteer 手动安装或在Pycharm 包中查找pyppeteer(设置-> 项目-> Python 解释器-> 点击+图标)
猜你喜欢
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 2022-08-21
  • 2019-12-06
  • 2017-11-06
  • 1970-01-01
相关资源
最近更新 更多