Printing and formatting results in BeautifulSoup

时间:2015-05-12 23:40:28

标签: python string beautifulsoup

My problem is that I want to print only this results with '1', not '-1', but when I use letters I just get '1' or '-1'. I know that is working but is there any function to print only this with '1', not number but whole line?

find()

Sample of results:

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))

Results that I want:

/category/23/zzz.html -1

/torrent_download/4188694/zzz.torrent 1

/torrent/4188694/zzz.html#comments -1

/torrent_download/4188710/zzz.torrent 1

1 个答案:

答案 0 :(得分:1)

在打印之前检查get.find:

for links in tmp:
    get  =  links.get('href').find("torrent_download")
    if get != -1:
        print(links.get('href'),  get)