如何使用BeautifulSoup从所有脚本中提取正确的脚本

2024-04-25 07:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用BS4从Kickstarter网页中提取一些信息:https://www.kickstarter.com/projects/louisalberry/louis-alberry-debut-album-uk-european-tour

项目信息位于其中一个脚本标记中:(伪代码)

...
<script>...</script>
<script>
window.current_ip = ...
...
window.current_project = "<I want this part>"
</script>
...

我的当前代码:

^{pr2}$

目前,我可以使用索引[4]来获得我想要的部分,但是由于我不确定这是否正确,我如何从正确的脚本标记中提取文本?在

谢谢!在


Tags: 代码https标记脚本com信息网页www
2条回答

这应该行得通(不是转储到json,如果需要的话,可以打印输出,哦,是的,记住要更改变量,我说的“chooseapath”和“if there any class add it here”):

 from bs4 import BeuatifulSoup
 import requests
 import json

website = requests.get("https://www.kickstarter.com/projects/louisalberry/louis-alberry-debut-album-uk-european-tour")
soup= BeautifulSoup(website.content, 'lxml')
mytext = soup.findAll("script", {"class": "If theres any class add it here, or else delete this part"})
save_path = 'CHOOSE A PATH'
ogname = "kickstarter_text.json"
completename = os.path.join(save_path, ogname)
with open(completename, "w") as output:
    json.dump(listofurls, output)

您可以收集所有脚本元素并循环。使用请求访问响应对象内容

from bs4 import BeautifulSoup
import requests
res = requests.get("https://www.kickstarter.com/projects/louisalberry/louis-alberry-debut-album-uk-european-tour")
soup = BeautifulSoup(res.content, 'lxml')
scripts = soup.select('script')
scripts = [script for script in scripts]
for script in scripts:
    if 'window.current_project' in script.text:
        print(script)

相关问题 更多 >