【问题标题】:Python project little issue I can't seem to figure out to print somethingPython 项目小问题,我似乎想不出要打印的东西
【发布时间】:2017-09-11 19:06:32
【问题描述】:

所以,我最近一直在使用 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()

【问题讨论】:

  • 那么您的问题是什么?请edit您的帖子在问题本身中显示您想要的内容。即使人们愿意从某个随机站点启用 JavaScript 只是为了弄清楚您在问什么(我不是),但由此产生的问答对于在链接失效后出现的任何人来说都是毫无价值的。
  • 外部链接不好,mkay

标签: python html beautifulsoup urllib


【解决方案1】:

我查看了从您的代码返回的结果,我认为您可以通过查找 &lt;cite&gt; 标签来大大减少您的代码:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 2021-11-29
    • 2020-06-04
    • 2019-03-24
    • 1970-01-01
    • 2012-12-01
    • 2015-10-25
    相关资源
    最近更新 更多