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 Python kullanıcıdan gelen veriye göre işlem yapma
5
●101
- 09-10-2020, 17:22:53Merhaba, 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.
- 09-10-2020, 17:26:14
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:33:49Hocam bu şekilde çalıştırıp ekrana 1 yazdığımda hata kodu dönüyor.hesapadim adlı üyeden alıntı: mesajı görüntüle
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 strHocam o yapıyı hiç bilmiyorum ama örnek verebilirseniz incelemek isterim.Jufasto adlı üyeden alıntı: mesajı görüntüle - 09-10-2020, 18:24:25