Python项目小问题我好像想不出打印什么

2024-04-26 04:45:23 发布

您现在位置: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()

Tags: httpsimportnetoslinklinkscontentrequests
1条回答
网友
1楼 · 发布于 2024-04-26 04:45:23

我查看了代码返回的结果,我认为通过查找<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)

相关问题 更多 >