使用美丽汤从href提取标题文本

时间:2019-10-09 15:34:14

标签: python beautifulsoup

我正在尝试制作一个简单的脚本来从链接标题中提取纯文本,但我不知道该怎么做。

from bs4 import BeautifulSoup
import requests

page = requests.get('https://livestream.com/watch/browse/lifestyle/live')
soup = BeautifulSoup(page.content, 'html.parser')
titl = soup.find_all("div", class_= 'owner_name_container ellipsis')
print(titl)

输出为:

[<div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/11436227">Karbala Satellite Channel</a>
</div>, <div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/2064453">Obieqtivi TV</a>
</div>, <div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/1257164">The AV Company</a>
</div>, <div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/75381">Condo Hotels Playa del Carmen</a>
</div>, <div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/3320102">Al Kawn Radio &amp; TV</a>
</div>, <div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/26764475">Z1 Televizija</a>
</div>, <div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/11436227">Karbala Satellite Channel</a>
</div>, <div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/4237681">TVmos.tv</a>
</div>, <div class="owner_name_container ellipsis">
      on <a class="owner_name" href="/accounts/3673755">TVTEC</a>

1 个答案:

答案 0 :(得分:1)

只需在元素上使用.text。在这种情况下,<a>标签中每个div标签的文本:

from bs4 import BeautifulSoup
import requests

page = requests.get('https://livestream.com/watch/browse/lifestyle/live')
soup = BeautifulSoup(page.content, 'html.parser')
titl = soup.find_all("div", class_= 'owner_name_container ellipsis')

for each in titl:
    print(each.find('a').text)

输出:

Obieqtivi TV
Karbala Satellite Channel
The AV Company
Condo Hotels Playa del Carmen
Al Kawn Radio & TV
Lake Tahoe TV
...