【问题标题】:Python get the text inside of "div" tagPython获取“div”标签内的文本
【发布时间】:2017-04-05 13:21:29
【问题描述】:

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

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

如何用python做到这一点?谢谢

【问题讨论】:

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


    【解决方案1】:

    假设您已经使用requests 获取html_source 并将其存储在变量s 中,您可以使用以下代码提取所需标签的文本(示例中为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)
    

    输出:

    'WordPress Userpro Remote File Upload Exploit'
    

    【讨论】:

      猜你喜欢
      • 2014-09-23
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多