<ul>
<li>
<div class="c_logo_box">
<a href="money-transfer-companies/ria-money-transfer/"><img src="http://www.compareremit.com/uploads/ria-logo11.png" style="height:57px;width:147px;" alt="RIA Money Transfer"></a>
<span class="rs"> <span class="txt13">₹</span> 61.24</span>
</div>
</li>
...
我想从&#39; alt = Ria Money Transfer&#39;中删除名称。来自61.24的评分。
到目前为止,我有这个Python代码:
#!/usr/bin/python
import requests
import re
from bs4 import BeautifulSoup
r = requests.get('http://www.compareremit.com')
data = r.text
soup = BeautifulSoup(data)
for rate in soup.find_all('li', re.compile('money')):
print rate.text
它什么都没给我。有人能告诉我我错过了什么吗?另外,我在查看我在for循环搜索中支持哪个元素时遇到问题,你能否澄清一下在这种情况下如何知道在for循环中指定什么条件?
答案 0 :(得分:1)
有多种方法可以达到元素。一种选择是依赖a
标记,href
包含ria-money-transfer
部分,然后获取包含费率的following span
element:
import re
from bs4 import BeautifulSoup
import requests
response = requests.get('http://www.compareremit.com')
soup = BeautifulSoup(response.content)
link = soup.find('div', class_='c_logo_box').find('a', href=re.compile(r'ria-money-transfer'))
print(link.img.get('alt'))
rate = link.find_next_sibling('span').text.split(' ')[-1]
print(rate)
打印:
RIA Money Transfer
61.24
答案 1 :(得分:1)
您的代码在逻辑上不正确。您可以通过多种方式执行此操作,请尝试此代码
#!的/ usr / bin中/ Python的
import requests
import re
from bs4 import BeautifulSoup
r = requests.get('http://www.compareremit.com')
data = r.text
soup = BeautifulSoup(data)
for rate in soup.find_all('div',{"class":"c_logo_box"}):
print rate.a.img['alt']
print rate.span.text