Find Multiple Tags in Beutifulsoup4 and insert them into one string

46 views Asked by At

A have a code that gets your pastebin's data


def user_key():
    user_key_data = {'api_dev_key': 'my-dev-key',
                     'api_user_name': 'my-username',
                     'api_user_password': 'my-password'}
    req = urllib.request.urlopen('https://pastebin.com/api/api_login.php',
                                 urllib.parse.urlencode(user_key_data).encode('utf-8'),
                                 timeout=7)
    return req.read().decode()


def user_pastes()
    data = data = {'api_dev_key': 'my_dev_key',
                   'api_user_key': user_key(),
                   'api_option': 'list'}
    req = urllib.request.urlopen('https://pastebin.com/api/api_post.php',
                                 urllib.parse.urlencode(data).encode('utf-8'), timeout=7)
    return req.read().decode()

Every Paste has a unique html tag e.g. url, title, paste key, etc. The Above code will print these out per paste. I made a code that only takes certain tags. the paste url, paste title and the paste key

    my_pastes = []
    src = user_pastes()
    soup = BeautifulSoup(src, 'html.parser')
    for paste in soup.findAll(['paste_url', 'paste_title', 'paste_key']):
        my_pastes.append(paste.text)
    print(my_pastes)

What I want is to join the url, title and key per paste together into one string. I tried using the .join method but it only joins the chars. (might not make sense but you'll see when you try it)

Unrelated to the problem. What I'll do once they're joined. split them again and put them in a PyQt5 table enter image description here

1

There are 1 answers

0
codeBit On

So This is kind of the answer but I'm still looking for a more simpler code

    title = []
    key = []
    url = []
    src = user_pastes()
    soup = BeautifulSoup(src, 'html.parser')
    for paste_title in soup.findAll('paste_title'):
        title.append(paste_title.text)
    for paste_key in soup.findAll('paste_key'):
        key.append(paste_key.text)
    for paste_url in soup.findAll('paste_url'):
        url.append(paste_url.text)
    for i in range(len(title)):
        print(title[i], key[i], url[i])

Maybe from this answer you'll get the idea of what I want to achieve since the post was kind of confusing since I can't really express what I want