Sign in

How to jump multiple times in Python redirection

freak0 edited in Fri, 19 Aug 2022

Title Description

In this link, click the third hyperlink to enter a similar page, and then click the third hyperlink to enter a similar page for 4 times. Look at the name of the third hyperlink in the fourth page. http://py4e-data.dr-chuck.net...

The source of the topic and my own ideas

I can only catch the hyperlink of the first page, but how to jump

Related codes

//Please paste the code text below (do not use pictures instead of codes)

import urllib.request , urllib.parse , urllib.errorfrom bs4 import BeautifulSoupimport ssl

ctx = ssl.create_ default_ context() ctx.check_ hostname = Falsectx.verify_ mode = ssl.CERT_ NONE

url = input('Enter - ')html = urllib.request.urlopen (url, context=ctx).read()soup = BeautifulSoup(html, ' html.parser ')

tags = soup (' a ') for tag in tags :

print(tag.get('href', None))

Our teacher has the following tips, but I don't know how to use them. $ python3 solution.pyEnter URL: http://py4e-data.dr-chuck.net...Enter count : 4Enter position: 3Retrieving: http://py4e-data.dr-chuck.net...Retrieving : http://py4e-data.dr-chuck.net...Retrieving : http://py4e-data.dr-chuck.net...Retrieving : http://py4e-data.dr-chuck.net...Retrieving : http://py4e-data.dr-chuck.net...

What are your expectations? What is the actual error message?

No idea at all. Looking forward to the salvation of the great God!

1 Replies
commented on Fri, 19 Aug 2022

Write down the code according to the meaning of the title

In [22]: import requests

In [23]: from bs4 import BeautifulSoup

In [24]: def return_url(url, loop_count, link_count):
    ...:     i = 0
    ...:     while i < loop_count:  # 循环次数
    ...:         r = requests.get(url)
    ...:         data = r.text
    ...:         soup = BeautifulSoup(data)
    ...:         count = 0
    ...:         for link in soup.find_all('a'):
    ...:             count += 1
    ...:             if count == link_count: # 第几个链接
    ...:                 url = link.get('href',None)
    ...:                 break
    ...:         i += 1
    ...:     return url