kaki
kaki

Reputation: 103

Python append to array and for loop for it

I am trying to insert to array some links then for loop them ( to enter them).

My code :

import requests
from requests_html import HTMLSession
import sys

links = []

link = "http://tvil.me"
pagedata = HTMLSession().get(link)
info = pagedata.html.find('#page-right', first=True)
sidra = info.find(".index-episode-caption")
for x in sidra:
    link = x.absolute_links
    links.append(link)


for i in links:
    print(i)
    ##page = HTMLSession().get(i)  - Not working because the response in bottom
    ##print(page.xpath("//*[contains(@id, 'change-season-')]/a"))

The response of print(i):

{'http://www.tvil.me/view/374/2/6/v/מסע_בזמן_Timeless.html'}
{'http://www.tvil.me/view/212/3/22/v/לוציפר_Lucifer.html'}
{'http://www.tvil.me/view/3048/1/7/v/תחנה_19_Station_19.html'}
{'http://www.tvil.me/view/3039/1/10/v/המגדלים_הגבוהים_The_Looming_Tower.html'}
{'http://www.tvil.me/view/109/5/6/v/עמק_הסיליקון_Silicon_Valley.html'}
{'http://www.tvil.me/view/68/5/11/v/ילדה_אבודה_Lost_Girl.html'}
{'http://www.tvil.me/view/556/1/20/v/שלדון_הצעיר_Young_Sheldon.html'}
{'http://www.tvil.me/view/74/4/6/v/מפרשים_שחורים_Black_Sails.html'}
{'http://www.tvil.me/view/360/2/2/v/ווסטוורלד_Westworld.html'}
[Finished in 3.7s]

Its printed with {' '} because this its not entering the link.. What should I do.

Upvotes: 3

Views: 8416

Answers (2)

Jan K
Jan K

Reputation: 4150

Your variable link seems to be a single element set. So I would recommend

link = x.absolute_links.pop()

Upvotes: 0

Nannan AV
Nannan AV

Reputation: 419

Can you try this?

{''} means its a set and you only want the element in the set. And pop gives that

for x in sidra:
    link = x.absolute_links.pop()
    links.append(link)

Upvotes: 3

Related Questions