-7
私はこのプログラムを書いています。その目的は、リンクリストの18番目のリンクを訪問し、新しいページに再び18番目のリンクを訪問することです。どうすればこのプログラムを簡略化できますか? Python
このプログラムは意図したとおりに動作しますが、少し繰り返しています。
私は、どのような機能も使用せずに、簡単にする方法について考えているのだろうかと思っていました。このプロセスを10回から100回繰り返す場合、これは非常に長くなるでしょう。
ありがとうございました!
# Note - this code must run in Python 2.x and you must download
# http://www.pythonlearn.com/code/BeautifulSoup.py
# Into the same folder as this program
import urllib
from BeautifulSoup import *
url = raw_input('Enter - ')
if len(url) < 1 :
url='http://python-data.dr-chuck.net/known_by_Oluwanifemi.html'
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# Retrieve all of the anchor tags
tags = soup('a')
urllist = list()
count = 0
loopcount = 0
for tag in tags:
count = count + 1
tg = tag.get('href', None)
if count == 18:
print count, tg
urllist.append(tg)
url2 = (urllist[0])
html2 = urllib.urlopen(url2).read()
soup2 = BeautifulSoup(html2)
tags2 = soup2('a')
count2 = 0
for tag2 in tags2:
count2 = count2 + 1
tg2 = tag2.get('href', None)
if count2 == 18:
print count2, tg2
urllist.append(tg2)
XPath以外を使用していますか? –
これはcodereview.stackexchange.com – RvdK
に属していますが、「まだ機能について学んだことはありません。 – usr2564301