你好,这是我想要从使用BeautifulSoup抓取第一个链接的代码。
视图-来源:https://www.binance.com/en/blog
我想要抓取这里的第一篇文章,所以它应该是"Trust Wallet Now Supports Lumens,4 More Tokens“
我正在尝试使用Python来实现这一点。
我使用这个代码,但是它抓取了所有的链接,我只想抓取第一个链接
with open('binanceblog1.html', 'w') as article:
before13 = requests.get("https://www.binance.com/en/blog", headers=headers2)
data1b = before13.text
xsoup2 = BeautifulSoup(data1b, "lxml")
for div in xsoup2.findAll('div', attrs={'class':'title sc-0 iaymVT'}):
before_set13 = div.find('a')['href']
我该怎么做呢?
发布于 2019-03-27 20:23:03
当您找到满意的结果时,您可以评估循环和break
中的情况。
for div in xsoup2.findAll('div', attrs={'class':'title sc-62mpio-0 iIymVT'}):
before_set13 = div.find('a')['href']
if before_set13 != '/en/blog':
break
print('skipping ' + before_set13)
print('grab ' + before_set13)
具有以下更改的代码的输出:
skipping /en/blog
grab /en/blog/317619349105270784/Trust-Wallet-Now-Supports-Stellar-Lumens-4-More-Tokens
发布于 2019-03-27 20:32:24
目前我能想到的最简单的解决方案就是使用break
,这是因为findAll
for div in xsoup2.findAll('div', attrs={'class':'title sc-62mpio-0 iIymVT'}):
before_set13 = div.find('a')['href']
break
对于第一个元素,您可以使用find
before_set13 = soup.find('div', attrs={'class':'title sc-62mpio-0 iIymVT'}).find('a')['href']
发布于 2019-03-27 20:35:49
尝试(从“阅读更多”按钮中提取href )
import requests
from bs4 import BeautifulSoup
r = requests.get('https://www.binance.com/en/blog')
soup = BeautifulSoup(r.text, "html.parser")
div = soup.find('div', attrs={'class': 'read-btn sc-62mpio-0 iIymVT'})
print(div.find('a')['href'])
https://stackoverflow.com/questions/55385481
复制相似问题