使用Beautifulsoup的python赋值中的以下链接

使用Beautifulsoup的python赋值中的以下链接,python,python-2.7,beautifulsoup,Python,Python 2.7,Beautifulsoup,我有一个python类的赋值,我必须从特定位置的特定链接开始,然后跟随该链接特定次数。假设第一个链接的位置为1。 这是链接: 我在定位链接时遇到问题,出现错误“索引超出范围”。有人能帮忙找出链接/位置的位置吗?这是我的代码: import urllib from BeautifulSoup import * url = raw_input('Enter - ') html = urllib.urlopen(url).read() soup = BeautifulSoup(html) coun

我有一个python类的赋值,我必须从特定位置的特定链接开始,然后跟随该链接特定次数。假设第一个链接的位置为1。 这是链接:

我在定位链接时遇到问题,出现错误“索引超出范围”。有人能帮忙找出链接/位置的位置吗?这是我的代码:

import urllib
from BeautifulSoup import *

url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
count = int(raw_input('Enter count: '))+1
position = int(raw_input('Enter position: '))


tags = soup('a')
tags_lst = list()
for tag in tags:
    needed_tag = tag.get('href', None)
    tags_lst.append(needed_tag)
    for i in range(0,count):
        print 'retrieving: ',tags_lst[position]
好的,我写了这段代码,它可以正常工作:

import urllib
from BeautifulSoup import *

url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
count = int(raw_input('Enter count: '))+1
position = int(raw_input('Enter position: '))


tags = soup('a')
tags_lst = list()
for tag in tags:
    needed_tag = tag.get('href', None)
    tags_lst.append(needed_tag)
for i in range(0,count):    
    print 'retrieving: ',tags_lst[position]
    position = position + 1

我仍然得到了其他链接,而不是示例中的链接,但是当我打印整个链接列表时,位置匹配,所以我不知道。非常奇怪。

您的BeautifulSoup导入错误。我认为它与您显示的代码不兼容。你的下环也很混乱。通过对完全检索到的URL列表进行切片,可以获得所需的URL列表

我已经在我的代码中硬编码了你的url,因为它比在每次运行中键入它更容易

试试这个:

import urllib
from bs4 import BeautifulSoup

#url = raw_input('Enter - ')
url = 'http://python-data.dr-chuck.net/known_by_Fikret.html'
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# print soup
count = int(raw_input('Enter count: '))+1
position = int(raw_input('Enter position: '))


tags = soup('a')
# next line gets count tags starting from position
my_tags = tags[position: position+count]
tags_lst = []
for tag in my_tags:
    needed_tag = tag.get('href', None)
    tags_lst.append(needed_tag)
print tags_lst

几乎所有此分配的解决方案都有两个部分来加载URL。相反,我定义了一个函数,用于打印任何给定url的相关链接

最初,函数将使用Fikret.html url作为输入。后续输入依赖于出现在所需位置的刷新URL。 重要的代码行是这样的:
url=allerretour(url)[position-1]
这将获得新的url,该url将为循环提供另一轮

import urllib
from bs4 import BeautifulSoup
url = 'http://py4e-data.dr-chuck.net/known_by_Fikret.html' # raw_input('Enter URL : ')

position = 3 # int(raw_input('Enter position : '))
count = 4 #int(raw_input('Enter count : '))

def allerretour(url):
    print('Retrieving: ' + url)
    soup = BeautifulSoup(urllib.urlopen(url).read())
    link = list()
    for tag in soup('a'):
        link.append(tag.get('href', None))
    return(link)


for x in range(1, count + 2):
    url = allerretour(url)[position-1]

[编辑:从评论中剪切并粘贴此行]嗨!我不得不做一个类似的练习,因为我有一些疑问,我找到了你的问题。这是我的代码,我认为它是有效的。我希望这对你有帮助

import urllib
from bs4 import BeautifulSoup

url = 'http://py4e-data.dr-chuck.net/known_by_Fikret.html'
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')
count = 8
position = 18
tags_lst = []

for x in xrange(count-1):
    tags = soup('a')
    my_tags = tags[position-1]
    needed_tag = my_tags.get('href', None)
    tags_lst.append(needed_tag)
    url = str(needed_tag)
    html = urllib.urlopen(url).read()
    soup = BeautifulSoup(html, 'html.parser')
这是我的解决方案:

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: ')
link_line = int(input("Enter position: ")) - 1 
relative to first link
count = int(input("Enter count: "))

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

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

这是我在Python 2.7中的答案:

import urllib
from BeautifulSoup import *

URL = raw_input("Enter the URL:") #Enter main URL
link_line = int(raw_input("Enter position:")) - 1 #The position of link relative to first link
count = int(raw_input("Enter count:")) #The number of times to be repeated

while count >= 0:
    html = urllib.urlopen(URL).read()
    soup = BeautifulSoup(html)
    tags = soup('a')
    print URL
    URL = tags[link_line].get("href", None)
    count = count - 1

下面是给出所需输出的工作代码

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
n=1
url = input('Enter - ')
count= int(input('Enter count'))+1
pos=int(input('Enter position'))
new=url
while n<count:
    if new == url:
        html = urllib.request.urlopen(url, context=ctx).read()
        print('Retrieving', url)
    html = urllib.request.urlopen(new, context=ctx).read()
    soup = BeautifulSoup(html, 'html.parser')
    tags = soup('a')
    my_tags=tags[pos-1]
    new=my_tags.get('href', None)
    print('Retrieving' , new)
    n=n+1
导入urllib.request、urllib.parse、urllib.error
从bs4导入BeautifulSoup
导入ssl
#忽略SSL证书错误
ctx=ssl.create\u default\u context()
ctx.check_hostname=False
ctx.verify_mode=ssl.CERT_NONE
n=1
url=input('Enter-')
计数=整数(输入('Enter count'))+1
pos=int(输入(‘输入位置’)
新建=url

而n我将解决方案放在下面,经过测试,目前效果良好

导入需要的模块 访问网站 检索所有锚定标记 试试这个。 您可以在输入URL后离开。这是你以前链接的示例。 祝你好运

import urllib.request
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 ')
cn = input('Enter count: ')
cnint = int(cn)
pos = input('Enter position: ')
posint = int(pos)
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/known_by_Fikret.html''''url''', context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')

tags_lst = list()
for x in range(0,cnint):
    tags = soup('a')
    my_tags = tags[posint-1]
    needed_tag = my_tags.get('href', None)
    url = str(needed_tag)
    html = urllib.request.urlopen(url,context=ctx).read()
    soup = BeautifulSoup(html, 'html.parser')
    print(my_tags.get('href', None))

完整显示您的回溯,如果可能,url是什么?它是公共的吗?我更新了描述你导入的bs错误<代码>来自bs4导入美化组
。但是你说的计数是什么意思?和位置。你是说从位置开始计算下一个链接吗?你应该检查你的标签列表是否有正确的长度,然后才能在任意位置访问它。Joel,任务的要点是找到一个特定的链接,然后从那里打印下一个链接的x号。然而,我似乎找不到如何找到具体的林奇的位置!我不得不做一个类似的练习,因为我有一些疑问,我找到了你的问题。这是我的代码,我认为它是有效的。我希望它会对你有所帮助。你可以考虑编辑来解释为什么和如何解决这个问题,以提高它的长期价值。你能给你的代码增加一些最小的解释吗?简要地解释你的答案。
url = "http://py4e-data.dr-chuck.net/known_by_Vairi.html"
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')
all_num_list = list()
link_position = 18
Process_repeat = 7
tags = soup('a')

while Process_repeat - 1  >= 0 :
    print("Process round", Process_repeat)
    target = tags[link_position - 1]
    print("target:", target)
    url = target.get('href', 2)
    print("Current url", url)
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html, 'html.parser')
    tags = soup('a')
    Process_repeat = Process_repeat - 1
import urllib.error, urllib.request
from bs4 import BeautifulSoup

#url = 'http://py4e-data.dr-chuck.net/known_by_Fikret.html'
url = input('Enter link - ')
count = int(input('Enter count - '))
position = int(input('position - ') )
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, 'html.parser')

tags = soup('a')
my_tags = tags[position-1]
needed_tag = my_tags.get('href', None)
print("------ : ", tags[position-1].contents[0])

for x in range(count-1):

    url = str(needed_tag)
    html = urllib.request.urlopen(url).read()
    soup = BeautifulSoup(html, 'html.parser')

    tags = soup('a')
    my_tags = tags[position-1]
    needed_tag = my_tags.get('href', None)
    print("------ : ", tags[position-1].contents[0])
import urllib.request
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 ')
cn = input('Enter count: ')
cnint = int(cn)
pos = input('Enter position: ')
posint = int(pos)
html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/known_by_Fikret.html''''url''', context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')

tags_lst = list()
for x in range(0,cnint):
    tags = soup('a')
    my_tags = tags[posint-1]
    needed_tag = my_tags.get('href', None)
    url = str(needed_tag)
    html = urllib.request.urlopen(url,context=ctx).read()
    soup = BeautifulSoup(html, 'html.parser')
    print(my_tags.get('href', None))