Python 跟踪网站链接的重复过程(BeautifulSoup):PY4E
我正在用Python编写一段代码,以使用BeautifulSoup获取URL中的所有“a”标记。这是我一直试图在Python3.7中使用的代码,但它一直从第一页开始打开链接,而不跟随链接。我是python新手,我一直在挠头想办法弄明白这一点。谁能告诉我我做错了什么?非常感谢您的帮助Python 跟踪网站链接的重复过程(BeautifulSoup):PY4E,python,loops,beautifulsoup,tags,coding-style,Python,Loops,Beautifulsoup,Tags,Coding Style,我正在用Python编写一段代码,以使用BeautifulSoup获取URL中的所有“a”标记。这是我一直试图在Python3.7中使用的代码,但它一直从第一页开始打开链接,而不跟随链接。我是python新手,我一直在挠头想办法弄明白这一点。谁能告诉我我做错了什么?非常感谢您的帮助 import urllib.request, urllib.parse, urllib.error from bs4 import BeautifulSoup import ssl # Ignore SSL cer
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 URL: ")
#html = urllib.request.urlopen(url, context=ctx).read()
#soup = BeautifulSoup(html, 'html.parser')
count = int(input('Enter count:'))
position = int(input('Enter Position:'))
lst1 = list()
lst2 = list()
lst_3 = list()
print("Retrieving:", url)
# Retrieve all of the anchor tags#
for i in range(count):
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
tags = soup('a')
for tag in tags:
h1= tag.get('href', None)
h2 = tag.contents[0]
lst1.append(h1)
lst2.append(h2)
y = lst1[position-1]
url = y
print('Retrieving:', url)
lst_3.append(y)
#print(lst1)
print(lst_3)