天天看點

Following Links in HTML Using BeautifulSoup

“”"
Created on Tue Aug 4 10:47:12 2020

@author: Judy
“”"

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = input('Enter URL: ')
count = int(input('Enter count: '))
position = int(input('Enter position: '))

while count > 0:
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, ‘html.parser’)
tags = soup(‘a’)
url = tags[position-1].get(‘href’,None)
print(url)
count = count -1
           

玄學,跑了一天總是卡循環,發給朋友看了看,然後就能跑出來了