使用 BeautifulSoup 获取属性值 [英] Getting attribute's value using BeautifulSoup

查看:45
本文介绍了使用 BeautifulSoup 获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 python 脚本,它将在从网页解析后提取脚本位置.假设有两种情况:

I'm writing a python script which will extract the script locations after parsing from a webpage. Lets say there are two scenarios :

<script type="text/javascript" src="http://example.com/something.js"></script>

<script>some JS</script>

我可以从第二个场景中获取 JS,也就是将 JS 写入标签中.

I'm able to get the JS from the second scenario, that is when the JS is written within the tags.

但是有什么办法,我可以从第一个场景中获取 src 的值(即提取脚本中 src 标签的所有值,例如 http://example.com/something.js)

But is there any way, I could get the value of src from the first scenario (i.e extracting all the values of src tags within script such as http://example.com/something.js)

这是我的代码

#!/usr/bin/python

import requests 
from bs4 import BeautifulSoup

r  = requests.get("http://rediff.com/")
data = r.text
soup = BeautifulSoup(data)
for n in soup.find_all('script'):
    print n 

输出:一些JS

需要的输出:http://example.com/something.js

推荐答案

仅当 src 值存在时,它才会获取所有的值.否则它会跳过那个

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆