我的问题是我只想打印带有“1”的结果,而不是“-1”,但是当我使用 find()
时,我只得到“1”或“-1” 。我知道这是有效的,但是有没有什么函数可以只用“1”打印这个,而不是数字而是整行?
import requests
import bs4
def links(url):
response = requests.get(url)
soup = bs4.BeautifulSoup(response.text)
tmp = soup.find_all('a')
for links in tmp:
print(links.get('href'), links.get('href').find("torrent_download"))
url="http://extratorrent.cc"
print(links(url))
结果示例:
/category/23/zzz.html -1
/torrent_download/4188694/zzz.torrent 1
/torrent/4188694/zzz.html#comments -1
/torrent_download/4188710/zzz.torrent 1
我想要的结果:
/torrent_download/4188710/zzz.torrent 1
/torrent_download/4188694/zzz.torrent 1
最佳答案
在打印之前检查 get.find:
for links in tmp:
get = links.get('href').find("torrent_download")
if get != -1:
print(links.get('href'), get)
关于python - 在 BeautifulSoup 中打印和格式化结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30203245/