Python BeautifulSoup,如何在标签和符号之间存储文本
Python BeautifulSoup, how to store text in between tags and symbols
from urllib import request
from bs4 import BeautifulSoup as bs
#Used these libs
我需要存储这两个 'texts' 我该怎么做,
<option value="/random-file/76/6/">Thing</option>
<option value="/random-file/36/6/">Thing1</option>
<option value="/random-file/50/4/">Thing2</option>
我只需要一种方法来提取和存储数据,"/random-file/76/6/" & "Thing"
将值存储在 dict
:
example = """<option value="/random-file/76/6/">Thing</option>
<option value="/random-file/36/6/">Thing1</option>
<option value="/random-file/50/4/">Thing2</option>"""
soup = BeautifulSoup(example, "html.parser")
options = {}
for o in soup.find_all("option"):
options[o["value"]] = o.text
print (options)
输出:
{u'/random-file/36/6/': u'Thing1', u'/random-file/76/6/': u'Thing', u'/random-file/50/4/': u'Thing2'}
from urllib import request
from bs4 import BeautifulSoup as bs
#Used these libs
我需要存储这两个 'texts' 我该怎么做,
<option value="/random-file/76/6/">Thing</option>
<option value="/random-file/36/6/">Thing1</option>
<option value="/random-file/50/4/">Thing2</option>
我只需要一种方法来提取和存储数据,"/random-file/76/6/" & "Thing"
将值存储在 dict
:
example = """<option value="/random-file/76/6/">Thing</option>
<option value="/random-file/36/6/">Thing1</option>
<option value="/random-file/50/4/">Thing2</option>"""
soup = BeautifulSoup(example, "html.parser")
options = {}
for o in soup.find_all("option"):
options[o["value"]] = o.text
print (options)
输出:
{u'/random-file/36/6/': u'Thing1', u'/random-file/76/6/': u'Thing', u'/random-file/50/4/': u'Thing2'}