Overdose013
Overdose013

Reputation: 3

Loop in python to get title tag from a URL

import urllib.request as urllib2
from bs4 import BeautifulSoup

a = "https://player.vimeo.com/video/1234"

soup = BeautifulSoup(urllib2.urlopen(a))
print (a + soup.title.string)

I want to get the titles of the URL with a looping which adds a number every time it gets the URL title.

eg: I get the title for https://player.vimeo.com/video/1234 then https://player.vimeo.com/video/1235 and so on..

Upvotes: 0

Views: 216

Answers (2)

Humayun Ahmad Rajib
Humayun Ahmad Rajib

Reputation: 1560

If you have more url, add into lst.You got all the title. you can try below script:

import urllib.request as urllib2
from bs4 import BeautifulSoup

lst = ["https://player.vimeo.com/video/1234","https://player.vimeo.com/video/1235"]
title = []
for a in lst:    
    soup = BeautifulSoup(urllib2.urlopen(a), 'lxml')
    title.append(soup.title.string)

print(title)

Output will be:

['Diving catch from Chris Bodenner on Vimeo', 'Hit with box from Chris Bodenner on Vimeo']

OR

import urllib.request as urllib2
from bs4 import BeautifulSoup

lst = ["https://player.vimeo.com/video/1234","https://player.vimeo.com/video/1235"]
title = []
for a in lst:    
    soup = BeautifulSoup(urllib2.urlopen(a), 'lxml')
    title.append(soup.title.string)
    print (a + " : " + soup.title.string)

Output will be:

https://player.vimeo.com/video/1234 : Diving catch from Chris Bodenner on Vimeo
https://player.vimeo.com/video/1235 : Hit with box from Chris Bodenner on Vimeo

Upvotes: 0

vicki
vicki

Reputation: 36

You can do it like this:

import urllib.request as urllib2
from bs4 import BeautifulSoup

start_idx, end_idx = 1234, 1245

for idx in range(start_idx, end_idx):
  a = f"https://player.vimeo.com/video/{idx}"
  soup = BeautifulSoup(urllib2.urlopen(a))
  print (f"for url:{a}, title: {soup.title.string}")

Set start_idx and end_idx correctly as you want.

Also you may want to deal with possible HTTPError coming due to forbiddedn access to some urls.

Upvotes: 2

Related Questions