抓取网址会导致“正在加载...”内容

问题描述 投票:0回答:1

我正在尝试读取 python 3.8 中的 url,但 html 内容仅显示以“正在加载...”结尾的内容

from urllib.request import urlopen
link='https://opencorporates.com/companies/us_fl/P97000018463'
page = urlopen(link)

html_bytes = page.read()
html = html_bytes.decode("utf-8")
html

结果

'<html><head><script type="text/javascript"><!--\nfunction leastFactor(n) {\n if (isNaN(n) || !isFinite(n)) return NaN;\n if (typeof phantom !== \'undefined\') return \'phantom\';\n if (typeof module !== \'undefined\' && module.exports) return \'node\';\n if (n==0) return 0;\n if (n%1 || n*n<2) return 1;\n if (n%2==0) return 2;\n if (n%3==0) return 3;\n if (n%5==0) return 5;\n var m=Math.sqrt(n);\n for (var i=7;i<=m;i+=30) {\n  if (n%i==0)      return i;\n  if (n%(i+4)==0)  return i+4;\n  if (n%(i+6)==0)  return i+6;\n  if (n%(i+10)==0) return i+10;\n  if (n%(i+12)==0) return i+12;\n  if (n%(i+16)==0) return i+16;\n  if (n%(i+22)==0) return i+22;\n  if (n%(i+24)==0) return i+24;\n }\n return n;\n}\nfunction go() {\n var p=1654161720790; var s=2297856402; var n;\nif ((s >> 15) & 1)\tp+=\n120411494*\t16;/*\np+= */else /* 120886108*\n*/p-=\t5952163*\t16;\tif ((s >> 1) & 1)\np+=185622079*\n2;/*\np+= */else \np-=/* 120886108*\n*/222069557*\t2; if ((s >> 12) & 1)/*\n*13;\n*/p+=/* 120886108*\n*/2437023*\t15;/*\np+= */else /*\np+= */p-=\n111288784*/*\nelse p-=\n*/13;\nif ((s >> 7) & 1)/*\np+= */p+=77307883*/*\n*13;\n*/8;\nelse /*\np+= */p-=\t86418547* 8;if ((s >> 9) & 1)\np+=/* 120886108*\n*/175293250* 10;\nelse \np-=57423209*/*\nelse p-=\n*/10; p-=2320115331;\n n=leastFactor(p);\n{ document.cookie="KEY="+n+"*"+p/n+":"+s+":3577604866:1;path=/;";\n  document.location.reload(true); }\n}\n//--></script></head>\n<body onload="go()">\nLoading...\n</body>\n</html>\n'

有没有办法用python读取实际的网页内容?

python web-scraping
1个回答
0
投票

要获取页面,您需要计算

KEY=
cookie 的值。您可以使用
js2py
模块来实现:

import re

import js2py
import requests
from bs4 import BeautifulSoup

url = "https://opencorporates.com/companies/us_fl/P97000018463"

with requests.session() as s:
    html_text = s.get(url).text
    script_text = re.search(r"<!--(.*)//-->", html_text, flags=re.S).group(1)
    script_text = script_text.replace("document.cookie=", "return ")
    KEY = js2py.eval_js(script_text)().split("=", maxsplit=1)[-1].split(";")[0]

    s.cookies["KEY"] = KEY

    soup = BeautifulSoup(s.get(url).content, "html.parser")

    # print company name:
    print(soup.h1.text)

打印:


J.G. IMPORT & EXPORT INC.

© www.soinside.com 2019 - 2024. All rights reserved.