2017-09-11 45 views
0

所以,我最近一直在用python冒险,而且我一直在尝试通过混合我发现和创建的代码来学习一些东西它将成为我未来可能最终使用的东西。我已经几乎完全的项目,虽然当我打印出来的链接,它说的是这样的事情,我宁愿Python项目小问题我似乎无法想出打印东西

https://v3rmillion.net/showthread.php

而不是:

https://v3rmillion.net/showthread.php?tid=393794

import requests,os,urllib,sys, webbrowser, bs4 

from bs4 import BeautifulSoup 

def startup(): 
    os.system('cls') 
    print('Discord To Profile') 
    user = raw_input('Discord Tag: ') 
    r = requests.get('https://www.google.ca/search?source=hp&q=' + user + ' site:v3rmillion.net') 
    soup = BeautifulSoup(r.text, "html.parser") 
    print soup.find('div',{'id':'resultStats'}).text 

    #This part below is where I'm having the issue. 
    content=r.content.decode('UTF-8','replace') 
    links=[] 
    while '<h3 class="r">' in content: 
     content=content.split('<h3 class="r">', 1)[1] 
     split_content=content.split('</h3>', 1) 
     link='http'+split_content[1].split(':http',1)[1].split('%',1)[0] 
     links.append(link) 
     #content=split_content[1] 
    for link in links[:5]: 
     print(link) 

startup() 
+1

那么你的_question_是什么?请[编辑]您的帖子,以显示您想要的问题本身。即使人们愿意从某个随机站点启用JavaScript来找出你所问的(我不是),那么在链接已经腐烂后,所得到的问答对任何人来说都是毫无价值的。 –

+0

它显示https://v3rmillion.net/showthread.php 当它只是应该只显示https://v3rmillion.net/showthread.php?tid=393794 – Biologic

+0

外部链接不好,mkay – JacobIRR

回答

0

我查看了您的代码返回的结果,我认为您可以通过查找<cite>标签大幅减少代码:

def startup(): 
    os.system('cls') 
    print('Discord To Profile') 
    user = raw_input('Discord Tag: ') 
    r = requests.get('https://www.google.ca/search?source=hp&q=' + user + ' site:v3rmillion.net') 
    soup = BeautifulSoup(r.text, "html.parser") 
    links=[] 
    for link in soup.find_all('cite'): 
     links.append(link.string) 
    for link in links[:5]: 
     print(link)