【发布时间】:2021-01-08 09:47:49
【问题描述】:
我正在尝试从trading website 中抓取数据。我从 python 'requests' 库开始,但它返回的 HTML 页面与我浏览器上的不同。
我观察到网页在加载缺少的信息时有轻微的延迟,在研究中,我发现这可以使用“requests-html”包解决。但是,“requests-html”库返回的 HTML 与“requests”相同。
我知道这可以通过使用 selenium 来解决,但是有没有办法使用上述库来解决这个问题?
这是我的代码
from bs4 import BeautifulSoup
import requests
import time
from requests_html import HTMLSession
with HTMLSession() as s:
login_url = 'https://www.screener.in/login/'
USERNAME = "username"
PASSWORD = "password"
s.get(login_url)
csrftoken = s.cookies['csrftoken']
login_data = dict(csrfmiddlewaretoken=csrftoken, next='', username=USERNAME, password=PASSWORD)
s.post(login_url, data=login_data, headers={"Referer": "https://www.screener.in/"})
r = s.get('https://www.screener.in/company/ABBOTINDIA/')
r.html.render(timeout=10, sleep=10)
print(r.html.html)
我哪里出错了?标题有问题吗?
我是网络抓取的新手,非常感谢您的帮助。
【问题讨论】:
-
您必须登录是否有特定原因?如果你去你的 ABBOTINDA 你仍然可以看到股票信息。此外,如果您得到的响应与您在浏览器中看到的不同,那么它是通过 JavaScript 显示的,您将不得不使用 Selenium。
-
@A.Patterson,用户可以根据自己的喜好添加一些财务比率(基本比率除外),用户需要登录才能访问其他比率
标签: python python-3.x web-scraping python-requests python-requests-html