python - 如何使用 python 通过 beautifulsoup 检索文本

标签 python selenium beautifulsoup

我想使用 beautifulsoup 从 HTML 中获取 Repairsonwheelsrim-hub.com 文本,请告诉我应该如何执行此操作。目前我正在使用

webadress = profilePageSource.select("span#offscreen a[href]")[0].get_text()


<div class="biz-website">
<span class="offscreen">Business website</span>
<a target="_blank" href="/biz_redir?url=http%3A%2F%2Frepairsonwheelsrim-hub.com&src_bizid=8tY2YtXPk1rGO7sl43LH8A&cachebuster=1438073532&s=6b75d47d32b28eb8e50506859857b75e949d698cdbc47e9892cc2a3b43e480c2">repairsonwheelsrim-hub.com</a>
</div>

最佳答案

这就是你想要的吗:

from bs4 import BeautifulSoup
text='<div class="biz-website"> <span class="offscreen">Business website</span> <a target="_blank" href="/biz_redir?url=http%3A%2F%2Frepairsonwheelsrim-hub.com&src_bizid=8tY2YtXPk1rGO7sl43LH8A&cachebuster=1438073532&s=6b75d47d32b28eb8e50506859857b75e949d698cdbc47e9892cc2a3b43e480c2">repairsonwheelsrim-hub.com</a> </div>'
soup = BeautifulSoup(text, 'html.parser')    
print soup.a.text

输出:

repairsonwheelsrim-hub.com

循环浏览 url 的文本:

from bs4 import BeautifulSoup
text='<div class="biz-website"> <span class="offscreen">Business website</span> <a target="_blank" href="/biz_redir?url=http%3A%2F%2Frepairsonwheelsrim-hub.com&src_bizid=8tY2YtXPk1rGO7sl43LH8A&cachebuster=1438073532&s=6b75d47d32b28eb8e50506859857b75e949d698cdbc47e9892cc2a3b43e480c2">repairsonwheelsrim-hub.com</a> </div>'    
soup = BeautifulSoup(text, 'html.parser')   
for t in soup.findAll("a"):
    print t.text

For more on BS4 see their official site

编辑:

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
a=requests.get("http://www.yelp.com/biz/scotts-pizza-tours-new-york")
text=a.content

soup = BeautifulSoup(text, 'html.parser')   
for t in soup.findAll(lambda tag: tag.name == 'a' and 'target' in tag.attrs):
    if "".join(t["target"]) in "_blank":
        print t.get_text()

输出:

scottspizzatours.com
scottspizzatours.com
scottspizzatours.com/pri…

关于python - 如何使用 python 通过 beautifulsoup 检索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673074/

相关文章:

python - 从已发布的 Power BI 视觉对象中抓取数据

python - 在多索引数据帧中 .columns.levels[1] 在 groupby 之后给出整个数据帧的列

python - 使用基本的低级 TensorFlow 训练循环训练 tf.keras 模型不起作用

Selenium - 定位具有相同类名的多个元素

java - webdriver,你能帮忙使用java获取选项吗?

python - 如何在使用 bs4 python 解析 xml 时保留 "和 '

python - 在没有索引的 for 循环中引用列表条目,可能吗?

Python os.path.relpath 行为

selenium - 使用 phantomjs/selenium 单击 Google OAuth2 批准按钮

python - 从网络中提取数据