-
Notifications
You must be signed in to change notification settings - Fork 0
/
urllink1.py
37 lines (30 loc) · 1015 Bytes
/
urllink1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# To run this, download the BeautifulSoup zip file
# http://www.py4e.com/code3/bs4.zip
# and unzip it in the same directory as this file
#to run this u can also install Beautiful Soup
#https://pypi.python.org/pypi/beautifulsoup4
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
cont=int(input('enter count: '))
line=int(input('enter position: '))
print('Retrieving: %s' % url)
for i in range(0,cont):
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
# Retrieve all of the anchor tags
tags = soup('a')
cn=0
ps=0
for tag in tags:
ps+=1
if ps==line:
print('Retrieving: %s' % str(tag.get('href',None)))
url=str(tag.get('href',None))
ps = 0
break