我有一个python类的作业,我必须从特定位置的特定链接开始,然后按照该链接特定次数。据说第一个链接的位置为1。 这是链接:http://python-data.dr-chuck.net/known_by_Fikret.html
traceback error picture 我找不到链接,错误"索引超出范围"出来。任何人都可以帮助找出如何找到链接/位置?这是我的代码:
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
我仍然获得除示例中的其他链接,但是当我打印整个链接列表时,位置匹配,所以我不知道。非常奇怪。
答案 0 :(得分: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')
答案 1 :(得分:0)
您的BeautifulSoup导入错误。我不认为它适用于您展示的代码。你的下部循环也令人困惑。您可以通过切片完整检索的列表来获取所需的网址列表。
我已经在我的代码中对您的网址进行了硬编码,因为它比在每次运行中输入更容易。
试试这个:
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
答案 2 :(得分:0)
此任务的几乎所有解决方案都有两个部分来加载网址。相反,我定义了一个函数,用于打印任何给定URL的相关链接。
最初,该函数将使用Fikret.html url作为输入。后续输入依赖于出现在所需位置的刷新URL。
重要的代码是这一行:url = allerretour(url)[position-1]
这会获得新的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]
答案 3 :(得分:0)
这是我的解决方案:
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
答案 4 :(得分:0)
这是我在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
答案 5 :(得分:0)
这是提供所需输出的工作代码
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
答案 6 :(得分:0)
我将解决方案放在下面,经过测试,直到今天为止工作良好。
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import re
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
答案 7 :(得分:0)
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])
答案 8 :(得分:0)
尝试一下。 您可以保留输入URL的权限。有您以前链接的示例。 祝你好运!
var a = ['barclays', 'google', 'vod'];
var b = [];
var dataLength = 4;
for (let i = 0; i < dataLength; i++) {
for (let o = 0; o < a.length; o++){
b.push(a[o]);
}
}
console.log(b);