繁体   English   中英

如何迭代链接并在特定位置访问一个?

[英]How iterate links and access one on specific position?

我正在做一个任务,我需要使用 BeautifulSoup 来解析它:http: //python-data.dr-chuck.net/known_by_Fikret.html

基本上,我需要打印初始 URL 并在位置 3 处找到 URL,访问该 URL 并在该页面上的位置 3 处找到链接,等等——这总共需要四次。

这是我到目前为止的代码:

# http://www.py4e.com/code3/bs4.zip
# and unzip it in the same directory as this file

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 =  "http://py4e-data.dr-chuck.net/known_by_Fikret.html"
timesToRepeat = '4'
positionInput = '3'
#timesToRepeat = input('Repeat how many times?: ')
#positionInput = input('Enter Position: ')
try:
    timesToRepeat = int(timesToRepeat)
    positionInput = int(positionInput)
except:
    print("please add an number")
    quit()

# Retrieve all of the anchor tags
totalCount = 0
currentRepetitionCount = 0

html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
tags = soup('a')

#Leave this all alone ^^^^
print("Retrieving: ",url)
for i in range(timesToRepeat):
    html = urllib.request.urlopen(url, context=ctx).read()
    for tag in tags:
        currentRepetitionCount += 1

        if not totalCount >= timesToRepeat:
            if currentRepetitionCount == positionInput:
                #print("current",currentRepetitionCount)
                #print("total",totalCount)
                #print("Retrieving: ",url)
                currentRepetitionCount = 0
                totalCount +=1
                url = tag.get('href', None)

                print("Retrieving: ",url)

我得到这个:

Retrieving:  http://py4e-data.dr-chuck.net/known_by_Fikret.html
Retrieving:  http://py4e-data.dr-chuck.net/known_by_Montgomery.html
Retrieving:  http://py4e-data.dr-chuck.net/known_by_Anona.html
Retrieving:  http://py4e-data.dr-chuck.net/known_by_Zoe.html
Retrieving:  http://py4e-data.dr-chuck.net/known_by_Carmyle.html

但我应该得到的是:

Retrieving: http://py4e-data.dr-chuck.net/known_by_Fikret.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Montgomery.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Mhairade.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Butchi.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Anayah.html

似乎链接没有改变,每次都只是在初始链接上找到第三个位置,我似乎无法终生修复它。

尝试简化您的代码,将重点放在您的问题和主要问题上。 因此,例如, if not totalCount >= timesToRepeat:

例子

请注意,在请求循环中的第一个url以避免重复时,我在timesToRepeat中添加了+1

from bs4 import BeautifulSoup
import requests

url = 'http://py4e-data.dr-chuck.net/known_by_Fikret.html'
timesToRepeat = 4
positionInput = 3

for i in range(timesToRepeat+1):
    print(f'Retrieving: {url}')
    soup=BeautifulSoup(requests.get(url).text)
    tag = soup.select('a')[positionInput-1]
    url = tag.get('href')
输出
Retrieving: http://py4e-data.dr-chuck.net/known_by_Fikret.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Montgomery.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Mhairade.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Butchi.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Anayah.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM