gpt4 book ai didi

javascript - 从 Python BeautifulSoup 中的 javascript 源中提取值

转载 作者:行者123 更新时间:2023-12-02 21:01:08 24 4
gpt4 key购买 nike

我对网络抓取还很陌生,想知道是否可以从 JavaScript 应用程序中提取我需要的信息。目前,我在 python 中使用 beautifulsoup,并对 html 解析器的输出感兴趣:

<p><script>
var acct = '488'; var loc = ''; var cat = ''; var stylesheet=''; var hideLastnames = true;
var jsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
document.write("<scr"+"ipt src='"+jsHost+"ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js' type='text/javascript'></scr"+"ipt>");
document.write("<scr"+"ipt>var jQuery = jQuery.noConflict(true);</scr"+"ipt>");
document.write("<scr"+"ipt src='"+jsHost+"www.groupexpro.com/schedule/embed/schedule_embed_responsive.js.php?a="+acct+"' type='text/javascript'></scr"+"ipt>");
</script></p>

在实际网站( https://recreation.gocrimson.com/fitness/schedules )中,它看起来像 this 。理想情况下,我想存储一个包含表中列出的所有信息的 json。有人有做过类似事情的经验吗?

最佳答案

https://recreation.gocrimson.com/fitness/schedules请求不同的 URL 以获取 JSONP 格式的计划数据。

网址:https://www.groupexpro.com/schedule/embed/json.php?schedule&instructor_id=true&format=jsonp&a=488&location=&category=&start=1587380400&end=1587898800

尝试理解该 URL 并将其修改为适合您的目的。

示例

from bs4 import BeautifulSoup
import requests
import json


headers ={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0"}

page=requests.get("https://www.groupexpro.com/schedule/embed/json.php?schedule&instructor_id=true&format=jsonp&a=488",headers=headers)

#Extract json from jsonp
jsondata='{'+page.text.split('{')[1].split('}')[0]+'}'

#can also be loaded into python dict using
data=json.loads(jsondata)

关于javascript - 从 Python BeautifulSoup 中的 javascript 源中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61352783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com