私はクローラープロジェクトに取り組んでいます。あるページのhrefテキストがそのドメインの他のページで繰り返され続ける状況で立ち往生しています。たとえば、URLがexample.comの場合、これらのページのhref値はhrefList = [/ hello / world、/ aboutus、/ blog、/contact]です。
したがって、これらのページのURLはexample.com/hello/worldexample.com/aboutusなどになります。
これで、example.com / hello / worldページに、hrefListが再び表示されます。したがって、URLはexample.com/hello/world/hello/world、example.com/hello/world/aboutusなどとして取得します。
これらのページのうち、/ hello / world / hello / worldはhttpステータスが200の適切なページであり、これは再帰的に発生しています。残りのページではページが見つからないため、破棄できます
正しいURLではない新しいURLのリストを取得しています。これを克服する方法はありますか?
これは私のコードベースです:
for url in allUrls:
if url not in visitedUrls:
visitedUrls.append(url)
http=httplib2.Http()
response,content=http.request(url,headers={'User-Agent':'Crawler-Project'})
if (response.status/100<4):
soup=BeautifulSoup(content)
links=soup.findAll('a',href=True)
for link in links:
if link.has_key('href'):
if len(link['href']) > 1:
if not any(x in link['href'] for x in ignoreUrls):
if link['href'][0]!="#":
if "http" in link["href"]:
allUrls.append(link["href"])
else:
if url[-1]=="/" and link['href'][0]=="/":
allUrls.append(url+link['href'][1:])
else:
if not (url[-1] =="/" or link['href'][0] =="/"):
allUrls.append(url+"/"+link['href'])
else:
allUrls.append(url+link['href'])