컴퓨터/Python

python ul li 크롤링 input

풍경소리^^ 2020. 10. 17. 18:34

import urllib.request

import urllib.parse

# query_url에 원하는 웹의 URL을 넣어주시면 됩니다.
query_url='https://www.amazon.com/bestsellers?ld=NSGoogle'

res=urllib.request.urlopen(query_url)

html = res.read()

soup = BeautifulSoup(html, 'html.parser')

result = soup.find('ul', id='zg_browseRoot').find('ul')

slist = result.find_all('li')

sec_list=[]
count=1
for li in slist:

    sec = li.get_text()

    sec_list.append(str(count)+"."+sec)

    count+=1

cnt=1
sec_input=[]
for i in sec_list:
    sec_input.append(i+'\t')

    if (cnt%3)==0:

        sec_input.append('\n')

    cnt+=1

sec_input.append('\n\n'+'1.위 분야 중에서 자료를 수집할 분야의 번호를  선택하세요: ')

sec=input(str(''.join(sec_input)))