想了解如何使用BeautifulSoup从内联样式中提取CSS属性的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于js获取内联样式的相关问题,此外,我们还将为您介绍关于LearnBeauti
想了解如何使用BeautifulSoup从内联样式中提取CSS属性的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于js获取内联样式的相关问题,此外,我们还将为您介绍关于Learn Beautiful Soup(5) —— 使用BeautifulSoup改变网页内容、python – 使用BeautifulSoup提取、Python-使用beautifulsoup提取属性值、Python:如何使用BeautifulSoup从HTML页面中提取URL?的新知识。
本文目录一览:- 如何使用BeautifulSoup从内联样式中提取CSS属性(js获取内联样式)
- Learn Beautiful Soup(5) —— 使用BeautifulSoup改变网页内容
- python – 使用BeautifulSoup提取
- Python-使用beautifulsoup提取属性值
- Python:如何使用BeautifulSoup从HTML页面中提取URL?
如何使用BeautifulSoup从内联样式中提取CSS属性(js获取内联样式)
我有这样的事情:
<img) src="notTheRealImage.jpg"/>
我正在使用beautifulsoup解析html。有没有办法拉出“背景” css属性中的“ URL”?
Learn Beautiful Soup(5) —— 使用BeautifulSoup改变网页内容
BeautifulSoup除了可以查找和定位网页内容,还可以修改网页。修改意味着可以增加或删除标签,改变标签名字,变更标签属性,改变文本内容等等。
使用修BeautifulSoup修改标签
每一个标签在BeautifulSoup里面都被当作一个标签对象,这个对象可以执行以下任务:
- 修改标签名
- 修改标签属性
- 增加新标签
- 删除存在的标签
- 修改标签的文本内容
修改标签的名字
只需要修改.name参数就可以修改标签名字。
producer_entries.name = "div"<span>怎么办嘛</span><img src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\~LWHD)}S}%DE5RTOO[CVEI1.gif" sysface="15"alt="" />
修改标签的属性
修改标签的属性如class,id,style等。因为属性以字典形式储存,所以改变标签属性就是简单的处理python的字典。
更新已经存在属性的标签
可以参照如下代码:
producer_entries[''id'']="producers_new_value"
为一个标签增加一个新的属性
比如一个标签没有class属性,那么可以参照如下代码增加class属性,
producer_entries[''class'']=''newclass''
删除标签属性
使用del操作符,示例如下:
del producer_entries[''class'']
增加一个新的标签
BeautifulSoup有new_tag()方法来创造一个新的标签。然后可以使用append(),insert(),insert_after()或者insert_before()等方法来对新标签进行插入。
增加一个新生产者,使用new_tag()然后append()
参照前面例子,生产者除了plants和alage外,我们现在添加一个phytoplankton.首先,需要先创造一个li标签。
用new_tag()创建一个新标签
new_tag()方法只能用于BeautifulSoup对象。现在创建一个li对象。
soup = BeautifulSoup(html_markup,"lxml")
new_li_tag = soup.new_tag("li")
new_tag()对象必须的参数是标签名,其他标签属性参数或其他参数都是可选参数。举例:
new_atag=soup.new_tag("a",href="www.example.com")
new_li_tag.attrs={''class'':''producerlist''}
append()方法添加新标签于,contents之后,就跟Python列表方法append()一样。
producer_entries = soup.ul
producer_entries.append(new_li_tag)
li标签是ul标签的子代,添加新标签后的输出结果。
<ul id="producers">
<li>
<div>
plants
</div>
<div>
100000
</div>
</li>
<li>
<div>
algae
</div>
<div>
100000
</div>
</li>s
<li>
</li>
</ul>
使用insert()向li标签中添加新的div标签
append()在.contents之后添加新标签,而insert()却不是如此。我们需要指定插入的位置。就跟python中的Insert()方法一样。
new_div_name_tag=soup.new_tag("div")
new_div_name_tag["class"]="name"
new_div_number_tag=soup.new_tag("div")
new_div_number_tag["class"]="number"
先是创建两个div标签
new_li_tag.insert(0,new_div_name_tag)
new_li_tag.insert(1,new_div_number_tag)
print(new_li_tag.prettify())
然后进行插入,输出效果如下:
<li class_="producerlist">
<div>
</div>
<div>
</div>
</li>
改变字符串内容
在上面例子中,只是添加了标签,但标签中却没有内容,如果想添加内容的话,BeautifulSoup也可以做到。
使用.string修改字符串内容
比如:
new_div_name_tag.string="phytoplankton"
print(producer_entries.prettify())
输出如下:
<ul id="producers">
<li>
<div>
plants
</div>
<div>
100000
</div>
</li>
<li>
<div>
algae
</div>
<div>
100000
</div>
</li>
<li>
<div>
phytoplankton
</div>
<div>
</div>
</li>
</ul>
使用.append/(),insert(),和new_string()添加字符串
使用append()和insert()的效果就跟用在添加新标签中一样。比如:
new_div_name_tag.append("producer")
print(soup.prettify())
输出:
<html>
<body>
<div>
<ul id="producers">
<li>
<div>
plants
</div>
<div>
100000
</div>
</li>
<li>
<div>
algae
</div>
<div>
100000
</div>
</li>
<li>
<strong><div>
phytoplankton
producer
</div>
</strong><div>
</div>
</li>
</ul>
</div>
</body>
</html>
new_string_toappend = soup.new_string("producer")
new_div_name_tag.append(new_string_toappend)
从网页中删除一个标签
删除标签的方法有decomose()和extract()方法
使用decompose()删除生产者
我们现在移去属性的div标签,使用decompose()方法。
third_producer = soup.find_all("li")[2]
div_name = third_producer.div
div_name.decompose()
print(third_producer.prettify())
输出:
<li class_="producerlist">
<div class_="number">
10000
</div>
</li>
decompose()方法会移去标签及标签的子代。
使用extract()删除生产者
extract()用于删除一个HTMNL文档中昂的标签或者字符串,另外,它还返回一个被删除掉的标签或字符串的句柄。不同于decompose(),extract也可以用于字符串。
third_producer_removed=third_producer.extract()
print(soup.prettify())
使用BeautifulSoup删除标签的内容
标签可以有一个NavigableString对象或tag对象作为子代。删除掉这些子代可以使用clear()
举例,可以移掉带有plants的div标签和 相应的class=number属性标签。
li_plants=soup.li
li_plants.clear()
输出:
<li></li>
可以看出跟li相关的标签内容被删除干净。
修改内容的特别函数
除了我们之前看到的那些方法,BeautifulSoup还有其他修改内容的方法。
- Insert_after()和Insert_before()方法:
这两个方法用于在标签或字符串之前或之后插入标签或字符串。这个方法需要的参数只有NavigavleString和tag对象。
soup = BeautifulSoup(html_markup,"lxml")
div_number = soup.find("div",class_="number")
div_ecosystem = soup.new_tag("div")
div_ecosystem[''class''] = "ecosystem"
div_ecosystem.append("soil")
div_number.insert_after(div_ecosystem)
print(soup.prettify())
输出:
<html>
<body>
<div>
<ul id="producers">
<li>
<div>
plants
</div>
<div>
100000
</div>
<div>
soil
</div>
</li>
<li>
<div>
algae
</div>
<div>
100000
</div>
</li>
</ul>
</div>
</body>
</html>
- replace_with()方法:
这个方法用于用一个新的标签或字符串替代原有的标签或字符串。这个方法把一个标签对象或字符串对象作为输入。replace_with()会返回一个被替代标签或字符串的句柄。
soup = BeautifulSoup(html_markup,"lxml")
div_name =soup.div
div_name.string.replace_with("phytoplankton")
print(soup.prettify())
replace_with()同样也可以用于完全的替换掉一个标签。
- wrap()和unwrap()方法:
wrap()方法用于在一个标签或字符串外包裹一个标签或字符串。比如可以用一个div标签包裹li标签里的全部内容。
li_tags = soup.find_all("li")
for li in li_tags:
<span> </span>new_divtag = soup.new_tag("div")
<span> </span>li.wrap(new_divtag)
print(soup.prettify())
python – 使用BeautifulSoup提取
1 /我正在尝试使用美丽的汤提取脚本的一部分,但它打印无.怎么了 ?
URL = "http://www.reuters.com/video/2014/08/30/woman-who-drank-restaurants-tainted-tea?videoId=341712453"
oururl= urllib2.urlopen(URL).read()
soup = BeautifulSoup(oururl)
for script in soup("script"):
script.extract()
list_of_scripts = soup.findAll("script")
print list_of_scripts
2 /目标是提取属性“transcript”的值:
总结
以上是小编为你收集整理的python – 使用BeautifulSoup提取全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
beautifulsoupbeautifulsoup
Python-使用beautifulsoup提取属性值
如何解决Python-使用beautifulsoup提取属性值?
.findAll()
返回所有找到的元素的列表,因此:
inputTag = soup.findAll(attrs={"name" : "stainfo"})
inputTag
是一个列表(可能仅包含一个元素)。根据你的确切要求,你应该执行以下操作:
output = inputTag[0][''value'']
或使用.find()
仅返回一个(第一个)找到的元素的方法:
inputTag = soup.find(attrs={"name": "stainfo"})
output = inputTag[''value'']
解决方法
我试图在网页上的特定“输入”标签中提取单个“值”属性的内容。我使用以下代码:
import urllib
f = urllib.urlopen("http://58.68.130.147")
s = f.read()
f.close()
from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(s)
inputTag = soup.findAll(attrs={"name" : "stainfo"})
output = inputTag[''value'']
print str(output)
我收到TypeError:列表索引必须是整数,而不是str
即使从Beautifulsoup文档中我了解到字符串在这里也不应该是一个问题…但是我没有专家,我可能会误解了。
Python:如何使用BeautifulSoup从HTML页面中提取URL?
我有一个包含多个div的HTML页面
angrape case who has sought shifting of t...
distribution companies – the Anil Ambani-owned BRPL and BYPL and the Tatas-owned Tata Powe...
我需要得到< a href =>具有类article-additional-info的所有div的值
我是BeautifulSoup的新手
所以我需要网址
"http://www.thehindu.com/news/national/gangrape-case-two-lawyers-claim-to-be-engaged-by-accused/article4332680.ece"
"http://www.thehindu.com/news/cities/Delhi/power-discoms-demand-yet-another-hike-in-charges/article4331482.ece"
实现这一目标的最佳方法是什么?
最佳答案
根据您的标准,它返回三个URL(而不是两个) – 您想要过滤掉第三个吗?
基本思想是迭代HTML,只抽取你的类中的那些元素,然后迭代该类中的所有链接,拉出实际的链接:
In [1]: from bs4 import BeautifulSoup
In [2]: html = # your HTML
In [3]: soup = BeautifulSoup(html)
In [4]: for item in soup.find_all(attrs={'class': 'article-additional-info'}):
...: for link in item.find_all('a'):
...: print link.get('href')
...:
http://www.thehindu.com/news/national/gangrape-case-two-lawyers-claim-to-be-engaged-by-accused/article4332680.ece
http://www.thehindu.com/news/cities/Delhi/power-discoms-demand-yet-another-hike-in-charges/article4331482.ece
http://www.thehindu.com/news/cities/Delhi/power-discoms-demand-yet-another-hike-in-charges/article4331482.ece#comments
这会将您的搜索范围限制为仅包含article-additional-info类标记的元素,并在其中查找所有锚点(a)标记并获取其相应的href链接.
我们今天的关于如何使用BeautifulSoup从内联样式中提取CSS属性和js获取内联样式的分享已经告一段落,感谢您的关注,如果您想了解更多关于Learn Beautiful Soup(5) —— 使用BeautifulSoup改变网页内容、python – 使用BeautifulSoup提取、Python-使用beautifulsoup提取属性值、Python:如何使用BeautifulSoup从HTML页面中提取URL?的相关信息,请在本站查询。
本文标签: