GAE의 핸들러에서 웹 크롤링 함수를 호출하고 몇 개의 이미지를 검색 한 다음 표시합니다. 첫 번째 호출에서는 정상적으로 작동하지만 다음에 동일한 이미지를 모두 표시하면 마지막 하나가 중단 된 곳에서 크롤러가 시작됩니다. 내 전역 변수가 제대로 재설정되지 않는 것이 문제라고 생각합니다.Google App Engine에서 전역 변수 재설정이 작동하지 않습니다.
매번 앱을 다시 배포 할 때 처음 제대로 수행하지만 문제가 시작됩니다.
여기에 제 코드가 나와 있습니다. 분명히 말하면 제게 알려주세요.하지만 이해가되어야한다고 생각합니다. 여기
은 스크레이퍼 기능을 호출하는 핸들러 스크레이퍼 기능 여기visited_pages = []
visit_queue = deque([])
collected_pages = []
collected_pics = []
count = 0
pic_count = 0
def scrape_pages(url, root_url, keywords=[], recurse=True):
#variables
max_count = 16
pic_num = 100
global count
global pic_count
global collected_pics
global collected_pages
print 'the keywords and url are'
print keywords
print url
#this is all of the links that have been scraped
the_links = []
soup = soupify_url(url)
#only add new pages onto the queue if the recursion argument is true
if recurse:
#find all the links on the page
try:
for tag in soup.findAll('a'):
the_links.append(tag.get('href'))
except AttributeError:
return
try:
external_links, internal_links, root_links, primary_links = categorize_links(the_links, url, root_url)
except TypeError:
return
#change it so this depends on the input
links_to_visit = external_links + internal_links + root_links
#build the queue
for link in links_to_visit:
if link not in visited_pages and link not in visit_queue:
visit_queue.append(link)
visited_pages.append(url)
count = count + 1
# print 'number of pages visited'
# print count
#add pages to collected_pages depending on the criteria given if any keywords are given
if keywords:
page_to_add = find_pages(url, soup, keywords)
# print 'page to add'
# print page_to_add
if page_to_add and page_to_add not in collected_pages:
collected_pages.append(page_to_add)
pics_to_add = add_pics(url, soup)
# print 'pics to add'
# print pics_to_add
if pics_to_add:
collected_pics.extend(pics_to_add)
#here is where the actual recursion happens by finishing the queue
while visit_queue:
if count >= max_count:
return
if pic_count > pic_num:
return
link = visit_queue.popleft()
# print link
scrape_pages(link, root_url, keywords)
# print '***done***'
###done with the recursive scraping function here
#here I just get a list of links from Bing, add them to the queue and go through them then reset all the global variables
def scrape_bing_src(keywords):
visit_queue, the_url = scrape_bing.get_links(keywords, a_list = False)
scrape_pages(visit_queue.popleft(), the_url, keywords, recurse=True)
global collected_pics
global pic_count
global count
global visited_pages
global visit_queue
pic_count = 0
count = 0
visited_pages = []
visit_queue = deque([])
pics_to_return = collected_pics
collected_pics = []
return pics_to_return
입니다
#this just simply displays the images
class Try(BlogHandler):
def get(self, keyword):
keyword = str(keyword)
keyword_list = keyword.split()
img_list = scraper.scrape_bing_src(keyword_list)
for img in img_list:
self.response.write("""<br><img src='""" + img + """'>""")
self.response.write('we are done here')
파이썬에서의 "Least Astonishment"의 복제본 : 변경 가능 기본 인수 (http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) –
위대한 리소스가 있지만 꽤 많은 정보가 있고 그 중 일부는 상충되는 것처럼 보입니다. 이러한 변수를 설정하는 것이 더 좋을 것이라고 생각하십니까? None 또는 원래 클래스 외부에서 호출하고 그냥 반환하는 함수를 설정하는 것이 좋을까요? – clifgray
두 가지 방법이 없습니다 : 함수 정의에'keywords = None'을 사용하십시오. –