如何从列表中提取一些特定的字符串并将它们存储在beautifulsoup中的变量中?
How to extract some specific strings from a list and store them in variables in beautifulsoup?
我想在包含多个标签(和字符串)的多个项目列表中提取特定字符串。并将它们存储到变量中。
from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.khanacademy.org/profile/DFletcher1990/')
r.html.render(sleep=5)
soup=BeautifulSoup(r.html.html,'html.parser')
user_socio_table=soup.find_all('div', class_='discussion-stat')
print(user_socio_table)
这是 print(user_socio_table)
的假定输出:
[<div class="discussion-stat">
4<span class="discussion-light"> questions</span>
</div>, <div class="discussion-stat">
444<span class="discussion-light"> votes</span>
</div>, <div class="discussion-stat">
718<span class="discussion-light"> answers</span>
</div>, <div class="discussion-stat">
15<span class="discussion-light"> flags raised</span>
</div>, <div class="discussion-stat">
10<span class="discussion-light"> project help requests</span>
</div>, <div class="discussion-stat">
38<span class="discussion-light"> project help replies</span>
</div>, <div class="discussion-stat">
208<span class="discussion-light"> comments</span>
</div>, <div class="discussion-stat">
11<span class="discussion-light"> tips and thanks</span>
</div>]
- 我想将
4
存储到名为 questions
、 的变量中
- 我想将
444
存储到名为 votes
、 的变量中
- 我想将
718
存储到名为 answers
、 的变量中
- 我想将
15
存储到名为 flags
、 的变量中
- 我想将
10
存储到名为 help_requests
、 的变量中
- 我想将
38
存储到名为 help_replies
、 的变量中
- 我想将
208
存储到名为 comments
、 的变量中
- 我想将
11
存储到一个名为 tips_thanks
的变量中。
感谢您的帮助!
你可以一个一个的取值,然后添加到json数组中
data = {}
for gettext in user_socio_table:
category = gettext.find('span')
category_text = category.text.strip() ## get text in span
number = category.previousSibling.strip() ## get value before span tag
data[category_text] = number ## add it
print(data)
输出:
{'questions': '4', 'votes': '444', 'answers': '718', 'flags raised': '15', 'project help requests': '10', 'project help replies': '38', 'comments': '208', 'tips and thanks': '11'}
你可以用特定的一个获得价值
print(data['questions'])
输出:
4
我想在包含多个标签(和字符串)的多个项目列表中提取特定字符串。并将它们存储到变量中。
from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.khanacademy.org/profile/DFletcher1990/')
r.html.render(sleep=5)
soup=BeautifulSoup(r.html.html,'html.parser')
user_socio_table=soup.find_all('div', class_='discussion-stat')
print(user_socio_table)
这是 print(user_socio_table)
的假定输出:
[<div class="discussion-stat">
4<span class="discussion-light"> questions</span>
</div>, <div class="discussion-stat">
444<span class="discussion-light"> votes</span>
</div>, <div class="discussion-stat">
718<span class="discussion-light"> answers</span>
</div>, <div class="discussion-stat">
15<span class="discussion-light"> flags raised</span>
</div>, <div class="discussion-stat">
10<span class="discussion-light"> project help requests</span>
</div>, <div class="discussion-stat">
38<span class="discussion-light"> project help replies</span>
</div>, <div class="discussion-stat">
208<span class="discussion-light"> comments</span>
</div>, <div class="discussion-stat">
11<span class="discussion-light"> tips and thanks</span>
</div>]
- 我想将
4
存储到名为questions
、 的变量中
- 我想将
444
存储到名为votes
、 的变量中
- 我想将
718
存储到名为answers
、 的变量中
- 我想将
15
存储到名为flags
、 的变量中
- 我想将
10
存储到名为help_requests
、 的变量中
- 我想将
38
存储到名为help_replies
、 的变量中
- 我想将
208
存储到名为comments
、 的变量中
- 我想将
11
存储到一个名为tips_thanks
的变量中。
感谢您的帮助!
你可以一个一个的取值,然后添加到json数组中
data = {}
for gettext in user_socio_table:
category = gettext.find('span')
category_text = category.text.strip() ## get text in span
number = category.previousSibling.strip() ## get value before span tag
data[category_text] = number ## add it
print(data)
输出:
{'questions': '4', 'votes': '444', 'answers': '718', 'flags raised': '15', 'project help requests': '10', 'project help replies': '38', 'comments': '208', 'tips and thanks': '11'}
你可以用特定的一个获得价值
print(data['questions'])
输出:
4