pythonpython-3.xweb-scrapingpython-requestspastebin

Read first line of a raw text


Hey Im using my little scraper and I want to save results with one detail, first line of a raw pastebin/any other bin webpage.

lets say I have this code:

r=requests.get("https://pastebin.com/raw/qH03hKGU") #random link
text=r.text

I want to get the first line of the variable text without saving it (I will save just the one line)


Solution

  • You can use partition('\n')[0] to get the first line:

    import requests
    
    r=requests.get("https://pastebin.com/raw/qH03hKGU") #random link
    text=r.text
    
    print(text.partition('\n')[0])
    OUT: import glob