Search code examples
pythonpython-requests-html

List Not Displaying The Values Only The Name For The Item


I am working on a little script to pull threads from TheTechGame.com and have it setup so it adds the information to a list but when I loop over that list to display the items in the terminal it just displays the names for the values instead of the title or link.

Code:

from requests_html import HTMLSession

session = HTMLSession()
r = session.get("https://thetechgame.com/Forums/f=4/offtopic-discussion.html")

topic_title = r.html.find("a.topic-title")

topic_list = []

for topic_name in topic_title:
    topic_info = {
        'title': topic_name.text,
        'link': topic_name.absolute_links
    }
    topic_list.append(topic_info)

for items in topic_list:
    print(' '.join(items)) 

Output:

title link
title link
...
title link
title link

I would like the title for the thread to be displayed topic_name.text and the link to show after that topic_name.absolute_links.


Solution

  • It looks like you need to access the values (instead of the names of the keys, as is currently happening within the .join() function). Something like this will give you the output it sounds like you are looking for. Here, you will iterate through each dictionary in the list, then access the values using the title key and the link key.

    for t in topic_list:
        print(t['title'], t['link']) 
    

    This will give you the following output:

    TheTechGame Special Award Holders + Special Award Tutorials {'https://www.thetechgame.com/Forums/t=7462502/thetechgame-special-award-holders-special-award-tutorials.html'}
    TTG All Time High Leaderboard {'https://www.thetechgame.com/Forums/t=7722177/ttg-all-time-high-leaderboard.html'}
    ...