Python项目没什么问题我似乎无法弄清楚打印的东西

时间:2017-09-11 19:06:32

标签: python html beautifulsoup urllib

所以,我最近一直在用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 个答案:

答案 0 :(得分: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)