繁体   English   中英

从 Python BeautifulSoup 中的 javascript 源中提取值

[英]Pull values from javascript source in Python BeautifulSoup

我对 web 抓取非常陌生,想知道是否可以从 javascript 应用程序中提取我需要的信息。 目前,我在 python 中使用 beautifulsoup 并对来自 ZFC35FDC70D5FC69D269EZ parserA8 的 output 感兴趣:

<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 )中,看起来是这样的。 理想情况下,我想存储一个 json 以及表中列出的所有信息。 有没有人做过类似的事情?

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

URL: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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM