如何从URL打印精确数据

时间:2018-03-31 15:47:24

标签: python url urllib

使用Python如何从以下URL打印request_token

https://kite.trade/?request_token=p87tOTSXRSp4O20TGr870n2JiXFKISIh&action=login&status=success

IE:=之后的&request_token之间的文字。

3 个答案:

答案 0 :(得分:0)

在python 2 https://docs.python.org/2/library/urlparse.html

中使用urlparse库

或者python 3中的urllib.parse https://docs.python.org/3/library/urllib.parse.html

答案 1 :(得分:0)

您可以使用字符串函数拆分字符串,并将查询放入dict中:

代码:

import pygame
from os import path

img_dir = path.join(path.dirname(__file__), "img")

width = 500
height = 600
fps = 30

# Cores 
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0 ,0)
green = (0, 255, 0)
blue = (0, 0, 255)
yellow = (255, 255, 0)

# Iniciar o game

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Trojan Clicker")
clock = pygame.time.Clock()


font_name = pygame.font.match_font("arial")
def draw_text(surf, text, size, x , y):
    font = pygame.font.Font(font_name, size)
    text_surface = font.render(text, True, white)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)


class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((75, 75))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = width / 2
        self.rect.bottom = height / 2
        self.speedx = 0

    def update(self):
        self.speedx = 0





all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)


clicks = 0
# Loop
running = True
while running:
    # Fps
    clock.tick(fps)
    # Eventos
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
                # 1 is the left mouse button, 2 is middle, 3 is right.
                if event.button == 1:
                    # `event.pos` is the mouse position.
                    if player.collidepoint(event.pos):
                        # Increment the number.
                        number += 1        



    # Updates
    all_sprites.update()


    # Draw / render X
    screen.fill(black)
    all_sprites.draw(screen)
    draw_text(screen, str(clicks), 18, width / 2, 10)

    # Depois de desenhar tudo, "flip" o display
    pygame.display.flip()

pygame.quit()    

您可以使用urllib执行相同的操作,如:

url = 'https://kite.trade/?request_token=p87tOTSXRSp4O20TGr870n2JiXFKISIh&' \
      'action=login&status=success'

query = dict(a.split('=') for a in url.split('?')[1].split('&'))
print(query)

结果:

import urllib
query = urllib.parse.parse_qs(urllib.parse.urlparse(url).query)
print(query['request_token'][0])

答案 2 :(得分:0)

我认为你所寻找的是来自parse_qs的{​​{1}}。

urllib.parse

Here is a link to the documentation for parse_qs.