BeautifulSoup 中去掉 获取文本内容

    • 参考
      • Beautifulsoup文档
      • 知乎网友方法
      • arsing unclosed br tags with BeautifulSoup


Data

>>> type(ips)
<class 'bs4.element.Tag'>
>>> print ips
<p>64.158.31.142:3128 美国 科罗拉多州布隆菲尔德市Level3通信公司
42.104.84.107:8080 印度 非大陆
110.37.216.6:8080 巴基斯坦 非大陆
54.70.50.55:3128 美国 新泽西州(Merck公司)
182.253.121.33:8080 印度尼西亚 非大陆

Code

>>> type(ips.find_all(text=True))
<class 'bs4.element.ResultSet'>
>>> res = ips.find_all(text=True)
>>> for str in res:
    print str

117.4.136.145:8080 越南  非大陆
188.166.83.6:1080 俄罗斯  非大陆
138.197.157.44:1080 美国  非大陆
83.56.123.0:3128 西班牙  非大陆
183.89.210.22:8080 泰国  非大陆
111.62.243.64:80 中国 移动

or

>>> for str in ips.descendants:
    if type(str) == type(ips):
        None
    else:
        print str.string

117.4.136.145:8080 越南  非大陆
188.166.83.6:1080 俄罗斯  非大陆
138.197.157.44:1080 美国  非大陆
83.56.123.0:3128 西班牙  非大陆
183.89.210.22:8080 泰国  非大陆
111.62.243.64:80 中国 移动

参考

1. Beautifulsoup文档

2. 知乎网友方法

3. arsing unclosed
tags with BeautifulSoup

你可能感兴趣的:(python)