Python获取“div”标签内的文本

时间:2017-04-05 13:21:29

标签: python-3.x beautifulsoup python-requests

我正在尝试发出请求并获取div标签内的所有字符串等:

            <div class='td allow_tip  ' ><h3><a href='/exploit/description/25950'>WordPress Userpro Remote File Upload Exploit</a></h3>

如何用python做到这一点? THX

1 个答案:

答案 0 :(得分:0)

假设您已使用requests获取html_source并将其存储在变量s中,则可以使用以下代码提取所需标记的文本({{1}在示例中):

<强>代码:

a tags

<强>输出:

from bs4 import BeautifulSoup

s = "<div class='td allow_tip  ' ><h3><a href='/exploit/description/25950'>WordPress Userpro Remote File Upload Exploit</a></h3>"

soup = BeautifulSoup(s, 'html.parser')

a_tags = soup.find_all('a')
for a in a_tags:
    print(a.text)