Python 使用Beauty soup从href中提取标题文本

Python 使用Beauty soup从href中提取标题文本,python,beautifulsoup,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_= 'ow

我正在尝试制作一个简单的脚本来从链接标题中提取纯文本,但我不知道如何做到这一点

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>
[
在…上
, 
在…上
, 
在…上
, 
在…上
, 
在…上
, 
在…上
, 
在…上
, 
在…上
, 
在…上

只需在元素上使用
.text
。在这种情况下,
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
...

只需在元素上使用
.text
。在这种情况下,来自
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
...

你试过看吗?它的基本知识覆盖得很好。你试过看吗?它的基本知识覆盖得很好。