我使用beauthulsoup,我得到带有标签('a')的url。在
现在我的问题是,我不知道如何得到一条线。在
我让用户输入一个特定的行position
,以及循环应该运行countrepeat
的次数。这将在代码中实现。在
所以我想不出两件事:
position
行(例如第三行)。在我的代码正在使用:
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 - ')
countrepeat = int(input("Enter repeat number:"))
position = int(input("Enter start position:"))
for i in range(countrepeat):
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
tags = soup('a')
print(tags.get('href'))
url = tags.get('href')
假设
position
是0索引的,使用soup.findAll('a')[position]
来获得汤中的第position
个锚。在要获得锚的href,请使用
soup.findAll('a')[position]['href']
。在要让循环在下一个请求中使用此href,请将循环中的
url
更新为来自锚点的href。在相关问题 更多 >
编程相关推荐