fuhrerguxez fuhrerguxez - 11 days ago 9
Python Question

Python - Format a string from a list not working

I want to crawl a webpage for some information and what I've done so far It's working but I need to do a request to another url from the website, I'm trying to format it but it's not working, this is what I have so far:

name = input("> ")

page = requests.get("http://www.mobafire.com/league-of-legends/champions")
tree = html.fromstring(page.content)

for index, champ in enumerate(champ_list):
if name == champ:
y = tree.xpath(".//*[@id='browse-build']/a[{}]/@href".format(index + 1))
print(y)

guide = requests.get("http://www.mobafire.com{}".format(y))
builds = html.fromstring(guide.content)
print(builds)

for title in builds.xpath(".//table[@class='browse-table']/tr[2]/td[2]/div[1]/a/text()"):
print(title)


From the input, the user enters a name; if the name matches one from a list (
champ_list
) it prints an url and from there it formats it to the
guide
variable and gets another information but I'm getting errors such as
invalid ipv6
.

This is the output url (one of them but they're similar anyway)
['/league-of-legends/champion/ivern-133']


I tried using slicing but it doesn't do anything, probably I'm using it wrong or it doesn't work in this case. I tried using replace as well, they don't work on lists; tried using it as:

y = [y.replace("'", "") for y in y]
so I could see if it removed at least the quotes but it didn't work neither; what can be another approach to format this properly?

Answer

I take it y is the list you want to insert into the string?

Try this:

"http://www.mobafire.com{}".format('/'.join(y))