Python:使用requestshtml的Web抓取不起作用

2024-09-28 17:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从trading website中提取数据。我从python“requests”库开始,但它返回的HTML页面与我浏览器上的页面不同

我注意到网页在加载丢失的信息时有一点延迟,通过研究,我发现这可以通过使用“requests html”包来解决。但是,“requests html”库返回与“requests”相同的html

我知道这可以通过使用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)

我哪里做错了?标题有问题吗

我对网页抓取还不熟悉,非常感谢您的帮助


Tags: infromhttpsimporturl网页datahtml