我想抓取链接: 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/