python - 使用Python进行异步请求爬取

标签 python web-crawler pyquery

我想抓取链接: http://data.eastmoney.com/hsgt/index.html

但是我发现XHR文档都是没有数据的,但是EventSteam,那么如何抓取页面的完整信息呢?

比如我要抓取页面-94.67亿元。

我的代码如下:

import requests
import pandas as pd
from pyquery import PyQuery
from lxml import etree
import time

response = requests.get(url='http://data.eastmoney.com/hsgt/index.html',
                        headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'})
response.encoding = 'GB2312'

# this shows False
'-94.67' in response.text

然后我尝试安装 dryscape 但失败了,它说我没有网络服务器文件。

非常感谢您的帮助。

最佳答案

正如您提到的,由客户端中运行的 JavaScript 管理的 XHR 请求并未被执行。这是因为 requests` 包不执行 javascript,也不试图模仿 Web 浏览器。您应该考虑另一种方法。有相当多。您有很多选择,我建议您阅读类似 the following 的页面有关该问题的更多背景信息。

此外,也许看看类似 dryscrape 的东西。我自己没用过,看起来类似于这个

import dryscrape

sess = dryscrape.Session()
sess.visit('http://data.eastmoney.com/hsgt/index.html')
source = sess.body()

就是你所追求的。玩得开心。

关于python - 使用Python进行异步请求爬取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60757596/

相关文章:

PHP爬取一个使用cloudflare的网站

python - pip 错误 : unrecognized command line option ‘-fstack-protector-strong’

python - 在 Windows 上运行 "lektor server"命令时出现问题

java - 在 Java 中实现这种模式的最佳方法(内部的 Python 示例)?

python - Django:更改模型表单中的字段类型,然后将其应用于模型类

python - 如何使用 Click 在 Python 中处理 CLI 的用户身份验证

Selenium 在 mac 上解释 javascript?

python - 我怎样才能使这个递归爬行函数迭代?

web-crawler - 从文章中提取作者

python - 解析lxml中的html正文片段