打印和格式化结果 BeautifulSoup
Printing and formatting results in BeautifulSoup
我的问题是我只想打印带有“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)
我的问题是我只想打印带有“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)