如何抓取这个网页的数据?

目标网页:
https://www.investing.com/equities/hoa-phat-group-jsc-ratios

image.png

抽取包含P/E Ratio的表。

我的尝试

import lxml.html
from urllib.request import urlopen
url = "https://www.investing.com/equities/hoa-phat-group-jsc-ratios"
file= urlopen(url).read()
 raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

查看它的network,一堆的get,post,我测试了许多都没有确定是哪个请求获得数据。

请问,如何抽取数据?

curl   https://www.investing.com/equities/hoa-phat-group-jsc-ratios
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
<script defer src="https://static.cloudflareinsights.com/beacon.min.js/vb26e4fa9e5134444860be286fd8771851679335129114" integrity="sha512-M3hN/6cva/SjwrOtyXeUa5IuCT0sedyfT+jK/OV+s+D0RnzrTfwjwJHhd+wYfMm9HJSrZ1IKksOdddLuN6KOzw==" data-cf-beacon='{"rayId":"7b1d7f836ae31e61","version":"2023.3.0","b":1,"token":"00ab903b5e184b1a9d53b0a7a5085300","si":100}' crossorigin="anonymous"></script>
</body></html>
阅读 2.3k
3 个回答

GET https://www.investing.com/equities/hoa-phat-group-jsc-ratios
就是当前URL的请求,服务端渲染返回的就是你需要的这部分数据

所以你需要做的就是解析这个响应中的HTML,可以考虑用scrapy框架处理全过程,或者是beautifulsoup单纯解析html响应体。

image.png


补充代码片段:

# -*- coding: utf-8 -*-
import urllib
from urllib.request import urlopen

url = "https://www.investing.com/equities/hoa-phat-group-jsc-ratios"

req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0')

r = urlopen(req).read().decode('utf-8')
with open("test.html", 'w', encoding="utf-8") as f:
    f.write(r)

用 puppeteer 或者浏览器插件吧,简单一些。

urllib.error.HTTPError: HTTP Error 403: Forbidden

这是因为你的 HTTP request 没有携带登录信息,所以服务器认为你是一个没有登录的用户或者没有权限的用户。

具体登录信息是 cookies 还是 token 还需要观察浏览器中的 request。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题