-4

I'd like to create a variable whose data is the title extracted from an URL without using any external module.

I'm new with Python so if you can, please explain what does every part of the code.

Thanks.

PD: I'm using Python 3.

PD2: I mean the title tag of its HTML.

Lectro
  • 25
  • 5
  • 1
    Just warning you, I think you should take down this question before it gets marked as a duplicate. You can find the answer [here](https://stackoverflow.com/questions/51233/how-can-i-retrieve-the-page-title-of-a-webpage-using-python) –  Apr 08 '18 at 04:21
  • 2
    @ToisMcBrois First, there is nothing wrong with having a question marked as a duplicate. Second, the OP does not want to use any external modules, and all answers to the question that you menion use external modules. – DYZ Apr 08 '18 at 04:32
  • 1
    A URL does not have a title. Do you actually want to extract the tag from a previously downloaded HTML file? – DYZ Apr 08 '18 at 04:34

2 Answers2

0

Assuming by "title", you mean the title of a resource: take a URL like https://www.foo.com/bar/baz/resource.jpg. You need to split it into a list along the /s, then take the last item in that list. The code

url = "https://www.foo.com/bar/baz/resource.jpg"
print(url.split('/')[-1])

gives the output

resource.jpg    
Ollie
  • 1,641
  • 1
  • 13
  • 31
0

Let html be an HTML string (say, the HTML source of this particular page). You can find the opening and closing tags with str.find(). The string is converted to the lower case to allow case-insensitive search.

start = html.lower().find('<title>') + len('<title>')
end = html.lower().find('</title>')

You can then extract the part of the HTML string between the tags:

html[start:end]
#'How can I extract the title from a URL in Python without using any...'
DYZ
  • 55,249
  • 10
  • 64
  • 93