테스트 사이트 - 개발 중인 베타 버전입니다

파이썬 코드 질문

써니쑤니 12개월 전 조회 4,452

from requests import get
value = '벨류값'
URL = '내사이트'


원본

print get('{}/경로/value={}'.format(URL, value).text.split('<!DOCTYPE html>')[0] 
 


수정

print (get('{}/경로/value={}'.format(URL, value).text.split('<!DOCTYPE html>')[0])


파이썬 코드가 현재 이런식으로 되어있는데 

 

막상 실행하면 코드는 적용되나, text split으로 보여주질 않습니다. 

혹시 제가 모르는 부분이나 잘못된 부분이 있다면 말씀부탁드립니다

* 수정한 이유는 파이썬최신에서 원본코드가 실행되지 않아 최신코드형태로 수정했습니다. 
 


 

댓글을 작성하려면 로그인이 필요합니다.

답변 3개

R
12개월 전

</p>

<pre>
print((get('{}/경로/value={}'.format(URL, value)).text.split('<!DOCTYPE html>')[0]))</pre>

<p>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

glitter0gim
12개월 전

※ 문자열 Data를 처리하지 못함일 것입니다 !

    >>> 경로나 값이 문제를 일으킬 수 있음

 

>>>  약간 다른 구성입니다.

</p>

<p>from requests import get, RequestException</p>

<p>value = 'values'

URL = 'My_site'</p>

<p>try:

    print(f"Requesting URL: {URL}/경로/value={value}")

    

    response = get(f"{URL}/경로/value={value}")

    

    if response.status_code == 200:

        if '<!DOCTYPE html>' in response.text:

            print(response.text.split('<!DOCTYPE html>')[0])

        else:

            print("Expected HTML format not found in response.")

    else:

        print(f"Request failed with status code: {response.status_code}")

        print("Check if the path or URL is correct.")

        

except RequestException as e:

    print("A network error occurred. Please check the network connection or the server status.")

    print(f"Error details: {e}")</p>

<p>

=== 참고만 하세요 ~

> 실패 이유가 경로 문제인지,

값 문제인지, 혹은 응답 형식의 문제인지 명확히 알 수 있을 것입니다.

 

 

 . . 

※ 실 사용으로, <!DOCTYPE html>태그 이전의 텍스트를 가져오는데 사용할 만한 소스.

</p>

<p>from requests import get, RequestException</p>

<p>value = 'values'

URL = 'My_site'</p>

<p>def fetch_content(url, path, value):

    full_url = f"{url}/{path}/value={value}"

    

    try:

        response = get(full_url, timeout=5)  

        if response.status_code == 200:

            content = response.text

            split_content = content.split('<!DOCTYPE html>', 1)[0]

            if split_content:

                return split_content

            else:

                return "No content found before <!DOCTYPE html>."

        else:

            return f"Request failed with status code: {response.status_code}."

            

    except RequestException as e:

        return f"Network error: {e}"

result = fetch_content(URL, '경로', value)

print(result) </p>

<p>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

12개월 전

확실치는 않은데, 이렇게 해보면 어떨지요..

(get 을 먼저하고, 그 결과의 텍스트를 split)

</p>

<p>print(get('{}/경로/value={}'.format(URL, value)).text.split('<!DOCTYPE html>')[0])</p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

고무타이어
12개월 전
해당 구문은 맞지않습니다.이미 프린트를 처리후 텍스트 스플릿 처리가 되어 오류가 나올겁니다.

프린트를 원하는 구문을 모두 묶어줘야하며

print (get('{}/경로/value={}'.format(URL, value).text.split('<!DOCTYPE html>')[0])

수정된 구문이 문제가 없어 보이는데 경로지정 혹은 벨류값 문제로 보여집니다.

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인