여러 요소의 문자열 목록을 반복하고 문자열을 다른 요소에 추가하려면 어떻게해야합니까? (파이썬)

aj96

저는 Python을 처음 접했습니다. 한 문자열의 요소를 다른 문자열에 추가하려고합니다. 아이디어는 URL에 다른 태그를 추가하는 것입니다. 이 URL은 코드에서 더 아래로 액세스 할 수있는 여러 URL의 목록 (또는 튜플)을 포함합니다. 이 코드는 웹 스크래핑 용입니다.

다음과 같이 달성 할 수있었습니다 (즉, 현재 코드 덩어리입니다).

def Commodoties(url, headings):
    a = ''
    for h in range(0, len(headings)):
        print(url, '/', headings[h], sep='')
        url = url + headings[h]
    return url, url

headings = ['currencies', 'commodities']
url = 'https://tradingeconomics.com/'

test = Commodoties(url, headings)
print(type(test))

이상적으로는 다음 라인을 따라 출력을 얻고 싶습니다.

['https://tradingeconomics.com/currencies', 'https://tradingeconomics.com/commodities']

현재 다음과 같은 결과를 얻었습니다.

('https://tradingeconomics.com/currenciescommodities', 'https://tradingeconomics.com/currenciescommodities')

Commodoties 기능에서 다른 방법을 구현했지만 원하는 결과를 얻을 수 없었습니다.

어떤 도움이라도 대단히 감사하겠습니다!

다니엘 하오

@ aj96,이 코드를 사용하여 원하는 형식인지 확인하십시오.

def Commodoties(url, headings):
    result = []
    for h in headings:
        # tmp = url + h
        result.append(url + h)   # simplify 
        print(result)            # can comment out; 
    return result

>>> headings = ['currencies', 'commodities']
>>> url = 'https://tradingeconomics.com/'
>>> test = Commodoties(url, headings)
['https://tradingeconomics.com/currencies']
['https://tradingeconomics.com/currencies', 'https://tradingeconomics.com/commodities']

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관