【发布时间】:2017-10-12 08:13:25
【问题描述】:
我正在尝试从 html 格式的网站上抓取网址。我用漂亮的汤。这是html的一部分。
<li style="display: block;">
<article itemscope itemtype="http://schema.org/Article">
<div class="col-md-3 col-sm-3 col-xs-12" >
<a href="/stroke?p=3083" class="article-image">
<img itemprop="image" src="/FileUploads/Post/3083.jpg?w=300&h=160&mode=crop" alt="Banana" title="Good for health">
</a>
</div>
<div class="col-md-9 col-sm-9 col-xs-12">
<div class="article-content">
<a href="/stroke">
<img src="/assets/home/v2016/img/icon/stroke.png" style="float:left;margin-right:5px;width: 4%;">
</a>
<a href="/stroke?p=3083" class="article-title">
<div>
<h4 itemprop="name" id="playground">
Banana Good for health </h4>
</div>
</a>
<div>
<div class="clear"></div>
<span itemprop="dateCreated" style="font-size:10pt;color:#777;">
<i class="fa fa-clock-o" aria-hidden="true"></i>
09/10 </span>
</div>
<p itemprop="description" class="hidden-phone">
<a href="/stroke?p=3083">
I love Banana.
</a>
</p>
</div>
</div>
</article>
</li>
我的代码:
from bs4 import BeautifulSoup
re=requests.get('http://xxxxxx')
bs=BeautifulSoup(re.text.encode('utf-8'), "html.parser")
for link in bs.find_all('a') :
if link.has_attr('href'):
print (link.attrs['href'])
结果将打印出此页面中的所有 url,但这不是我要找的,我只想要一个特定的,比如在这个例子中的 "/stroke?p=3083" 我如何设置条件Python? (我知道这里一共有三个“/stroke?p=3083”,但我只需要一个)
另一个问题。这个网址不完整,我需要将它们与“http://www.abcde.com”结合起来,所以结果将是“http://www.abcde.com/stroke?p=3083”。我知道我可以在 R 中使用 paste,但是如何在 Python 中做到这一点?提前致谢! :)
【问题讨论】:
标签: python url web-scraping beautifulsoup