• 09-10-2020, 17:22:53
    #1
    Merhaba, python ile bir şeyler yapmaya çalışıyorum. Aşağıdaki şekilde bir kod yazdım, istediğim şey şu. En son print kısmında ülkeleri listeliyorum, kullanıcıya ekranda hangi url ye girmek istediğini nasıl yazdırabilirim. Yani kullanıcı inputa 1 yazdığında 1. sıradaki ülkenin url si ile işlem yapmak istiyorum. Kullanıcı 1, 2 yazarsa 1. ve 2. sıradaki ülkenin url si ile işlem yapmak istiyorum. Yardımlarınızı bekliyorum. input kullanarak kullanıcıdan veri almayı öğrendim ancak burada nasıl kullanacağımı oturtamadım.

    import requests
    import os.path
    from pathlib import Path
    from bs4 import BeautifulSoup
    
    filename = '/var/python/liste.txt'
    
    if not os.path.exists(filename):
    open(filename, 'w').close()
    
    # Using readlines()
    # file = open(filename, 'r')
    # lines = file.readlines()
    
    # count = 0
    # Strips the newline character
    # for line in lines:
    # print("{}: {}" . format(count, line.strip()))
    # count += 1
    
    url = "https://www.asdfghgj.com"
    
    html = requests.get(url).content
    soup = BeautifulSoup(html, "html.parser")
    
    # list = soup.find('div', class_="footer-middle").findAll('a')
    count = 0
    for a in soup.select('.footer-middle a'):
    ulkeUrl = a['href']
    ulkeAdi = a.get_text(strip = True)
    print(count, ':', ulkeUrl, '/', ulkeAdi)
        count += 1
  • 09-10-2020, 17:26:14
    #2
    import requests
    import os.path
    from pathlib import Path
    from bs4 import BeautifulSoup
    
    filename = '/var/python/liste.txt'
    
    if not os.path.exists(filename):
    open(filename, 'w').close()
    
    # Using readlines()
    # file = open(filename, 'r')
    # lines = file.readlines()
    
    # count = 0
    # Strips the newline character
    # for line in lines:
    # print("{}: {}" . format(count, line.strip()))
    # count += 1
    
    url = "https://www.asdfghgj.com"
    
    html = requests.get(url).content
    soup = BeautifulSoup(html, "html.parser")
    
    # list = soup.find('div', class_="footer-middle").findAll('a')
    count = 0
    for a in soup.select('.footer-middle a'):
    ....ulkeUrl = a['href']
    ....ulkeAdi = a.get_text(strip = True)
    ....print(count, ':', ulkeUrl, '/', ulkeAdi)
    ....count += 1
    
    
    ulkeler = input('ulke seciniz: ').replace(' ', '').split(',')
    
    for ulkei in ulkeler:
    ....elm = soup.select('.footer-middle a')[ulkei]
    ....print(elm['href'])
    r10'da tab yapamıyorum, 4 nokta = tab
  • 09-10-2020, 17:27:50
    #3
    Her ülke için bir class oluştur ve her class'ın içerisine yapacağın işlemleri yaz. Daha sonrasında kullanıcıdan aldığın inputa göre if elif yapısıyla class'lara gönder.
  • 09-10-2020, 17:33:49
    #4
    hesapadim adlı üyeden alıntı: mesajı görüntüle
    import requests
    import os.path
    from pathlib import Path
    from bs4 import BeautifulSoup
    
    filename = '/var/python/liste.txt'
    
    if not os.path.exists(filename):
    open(filename, 'w').close()
    
    # Using readlines()
    # file = open(filename, 'r')
    # lines = file.readlines()
    
    # count = 0
    # Strips the newline character
    # for line in lines:
    # print("{}: {}" . format(count, line.strip()))
    # count += 1
    
    url = "https://www.asdfghgj.com"
    
    html = requests.get(url).content
    soup = BeautifulSoup(html, "html.parser")
    
    # list = soup.find('div', class_="footer-middle").findAll('a')
    count = 0
    for a in soup.select('.footer-middle a'):
    ....ulkeUrl = a['href']
    ....ulkeAdi = a.get_text(strip = True)
    ....print(count, ':', ulkeUrl, '/', ulkeAdi)
    ....count += 1
    
    
    ulkeler = input('ulke seciniz: ').replace(' ', '').split(',')
    
    for ulkei in ulkeler:
    ....elm = soup.select('.footer-middle a')[ulkei]
    ....print(elm['href'])
    r10'da tab yapamıyorum, 4 nokta = tab
    Hocam bu şekilde çalıştırıp ekrana 1 yazdığımda hata kodu dönüyor.

    Original exception was:
    Traceback (most recent call last):
    File "/var/python/testing.py", line 37, in <module>
    elm = soup.select('.footer-middle a')[ulkei]
    TypeError: list indices must be integers or slices, not str
    Jufasto adlı üyeden alıntı: mesajı görüntüle
    Her ülke için bir class oluştur ve her class'ın içerisine yapacağın işlemleri yaz. Daha sonrasında kullanıcıdan aldığın inputa göre if elif yapısıyla class'lara gönder.
    Hocam o yapıyı hiç bilmiyorum ama örnek verebilirseniz incelemek isterim.
  • 09-10-2020, 17:44:51
    #5
    Hocam ülke ad ve linkleri 2 farklı liste içerisine kaydet. daha sonra liste[kullanicidegeri-1] şeklinde yazabilirsiniz
  • 09-10-2020, 18:24:25
    #6
    Zalmoxis adlı üyeden alıntı: mesajı görüntüle
    Hocam bu şekilde çalıştırıp ekrana 1 yazdığımda hata kodu dönüyor.

    Original exception was:
    Traceback (most recent call last):
    File "/var/python/testing.py", line 37, in <module>
    elm = soup.select('.footer-middle a')[ulkei]
    TypeError: list indices must be integers or slices, not str
    Hocam o yapıyı hiç bilmiyorum ama örnek verebilirseniz incelemek isterim.
    ulkei
    yerine
    int(ulkei)
    yapın hocam