python - Link Fetching List -
so i've asked many questions regarding 1 subject, , i'm sorry. it.
so have code:
import urllib import urllib.request bs4 import beautifulsoup import sys collections import defaultdict m_num=int(input('enter number of monsters up: ')) x in range(m_num): name=input("enter monster's name: ") url_name=name.replace(' ','_') url=('http://yugioh.wikia.com/wiki/card_tips:{}'.format(url_name)) page = urllib.request.urlopen(url) soup = beautifulsoup(page.read()) content = soup.find('div',id='mw-content-text') links = content.findall('a') link_lists = defaultdict(list) link in links: link_lists[x].append(link.get('title')) all_lists = list(link_lists.values()) common_links = set(all_lists[0]).intersection(*all_lists[1:]) print('common links: ',common_links)
what i'm trying how many number of monsters user specifies how many lists creatd. each list filled links specific site. , in ned lists compared see if have similar strings inside of them. (hopefully makes sense).
so problem i'm having when run , gets print('common links:',common_links)
part prints out last list. doesn't compare lists nor recognize other lists created.
can lend helping hand? i've been troubleshooting , i'm stuck.
link_lists
refers new dictionary on each iteration. exclude it: put all_lists = []
before for x in range(m_num)
loop. , replace last 3 line in loop with: all_lists.append([link.get("title") link in links])
note: don't need know m_num
in case:
all_lists = [] name in iter(lambda: input("monster name"), ""): # loop until empty name # ... titles = [link["title"] link in content.findall('a', title=true)] all_lists.append(titles)
Comments
Post a Comment