• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

How to: Write to a Text File Python

i have a script created recently and i would like copy information on a text files and add a information

Code

from bs4 import BeautifulSoup
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

# initialize session
session = requests.Session()

# getting playlist
response = session.get('http://animedigitalnetwork.fr/video/naruto-shippuden', headers=headers)
soup = BeautifulSoup(response.content)
playlist = soup.find('a', {'data-playlist': True})['data-playlist']

# getting list of videos
url = 'http://animedigitalnetwork.fr/index.php?option=com_vodvideo&view=playlist&format=raw'
response = session.post(url, data={
    'playlist': playlist,
    'season': '',
    'order': 'DESC'
}, headers=headers)

soup = BeautifulSoup(response.content)
for video in soup.select('div.adn-video'):
    print video.a.get('href')

Open in new window


Demo
the code generate a list of link

/video/naruto-shippuden/3809-episode-7-cours-kankuro

Open in new window


and i would like add this information

http://www.animedigitalnetwork.fr

Open in new window


to get this on a text files

http://www.animedigitalnetwork.fr/video/naruto-shippuden/3809-episode-7-cours-kankuro

Open in new window


ty :)
0
Gaaara
Asked:
Gaaara
  • 13
  • 10
1 Solution
 
peprCommented:
Just replace the last three lines by
soup = BeautifulSoup(response.content)
with open('links.txt', 'w') as f:
    for video in soup.select('div.adn-video'):
        s = 'http://www.animedigitalnetwork.fr' + video.a.get('href')
        f.write(s + '\n')

Open in new window

The s = 'http://www.animedigitalnetwork.fr' + video.a.get('href') prepends the wanted prefix. Now the link is in the s variable. The f.write(s + '\n') writes the line to the open text file (plus newline to separate the links to the lines). The open('links.txt', 'w') opens the links.txt file for writing in the text mode. The with open('links.txt', 'w') as f: wraps it so that the open file object is named f, and ensures that the file is closed automatically after the construct.
0
 
GaaaraAuthor Commented:
it work and i forget a question

I would want to know how to add a link in a line of command(order) example python test.py link

i add this on the script

urladn =sys.argv

Open in new window

and how to a a variable on this line
response = session.get('variable', headers=headers)
0
 
peprCommented:
I am not sure if I understand you well. Do you mean something like this?
#!python2

from bs4 import BeautifulSoup
import os
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}

# initialize session
session = requests.Session()

# getting playlist
response = session.get('http://animedigitalnetwork.fr/video/naruto-shippuden', headers=headers)
soup = BeautifulSoup(response.content)
playlist = soup.find('a', {'data-playlist': True})['data-playlist']

# getting list of videos
url = 'http://animedigitalnetwork.fr/index.php?option=com_vodvideo&view=playlist&format=raw'
response = session.post(url, data={
    'playlist': playlist,
    'season': '',
    'order': 'DESC'
}, headers=headers)

soup = BeautifulSoup(response.content)
for video in soup.select('div.adn-video'):
    remote_path = video.a.get('href')
    url = 'http://www.animedigitalnetwork.fr' + remote_path

    # Get the content of the url.
    response = session.get(url,
        data={
            'playlist': playlist,
            'season': '',
            'order': 'DESC'},
        headers=headers)

    # Transform the remote name to the local name.
    local_path, basename = os.path.split(remote_path)
    local_path = 'responses' + local_path
    fname = os.path.join(local_path, basename)
    print basename

    # Make the local directory if it does not exist.
    if not os.path.isdir(local_path):
        os.makedirs(local_path)

    # Write the response content to the local file.
    with open(fname, 'w') as f:
        f.write(response.content)

Open in new window

It creates the responses subdirectory with the response contents stored in the separate files.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
GaaaraAuthor Commented:
no :)

ce que je veut faire c'est créer un input directement dans  le terminal
 I want to make a input directly in the terminal

exemple
python test.py -l http://animedigitalnetwork.fr/video/naruto-shippuden -n naruto

Open in new window


is a good idea for the name automatically the text files :)
0
 
peprCommented:
OK. I do not speak French ;)

I assume that you want to add the -l option to pass the URL from command line instead of having it explicitly written in the script, right?

What the -n option means? And what should be generated by the script?
0
 
GaaaraAuthor Commented:
option n is to personalise a name for text files
0
 
GaaaraAuthor Commented:
i dont no have a news ?
0
 
peprCommented:
You can import the sys module and use sys.argv to get the command line arguments. It is easier to use fixed positions of the arguments (without options -l and -n). See https://docs.python.org/2.7/library/sys.html#sys.argv
0
 
GaaaraAuthor Commented:
ty
0
 
peprCommented:
Do you understand what to do with the sys.argv? If not, continue to ask here.

Basically, you should first test if there is enough arguments passed to the script, and then you extract the wanted values into variables that will be later used instead of hardcoded strings:
if len(sys.argv) < 3:
    print 'Usage:', sys.argv[0], '<URL> <directory>'

givenURL = sys.argv[1]
outputdir = sys.argv[2]

Open in new window

0
 
GaaaraAuthor Commented:
I accepted the answer because you'd give me documentation and so I would analyze it to continue :)


thank you for the explanation ^^
0
 
peprCommented:
Feel free to continue here when you have the question related to the code.
0
 
GaaaraAuthor Commented:
no problem :) thank you
0
 
GaaaraAuthor Commented:
I have successfully completed the script :)
0
 
GaaaraAuthor Commented:
hello

how to print the First line  on a text files with python  ?
0
 
peprCommented:
The easy way is to read the file in the loop and use the break command when you want to stop:
#!python2

# Generate the demo file.
fname = 'testdata.txt'
with open(fname, 'w') as f:
    for n in xrange(1, 10):
        f.write('line {}\n'.format(n))


# Read the file, and break after printing the first line.        
with open(fname) as f:
    for line in f:
        print line.rstrip()
        break

Open in new window

0
 
GaaaraAuthor Commented:
cool ty :)
0
 
peprCommented:
It can be easily modified to get the first n lines:
# Read the file, and break after printing the first n lines.        
with open(fname) as f:
    for n, line in enumerate(f, 1):
        print line.rstrip()
        if n == 5:  # break after the fifth line
            break

Open in new window


The built-in enumerate() function adds counting of anything that can be iterated. Here the optional second argument was used to count lines from 1 instead from the default zero.
0
 
GaaaraAuthor Commented:
hello you have a complete website to learn python ?
0
 
peprCommented:
No. I do recommend the official web pages to find some. The official tutorial is good for beginners.
0
 
GaaaraAuthor Commented:
hello i have 2 question for you is the and of my script ! :)


my exemple is a bash :)  i would like get the same ting with python

[ "$(echo $REPLY | tr [:upper:] [:lower:])" == "y" ] || exit
           links=$(awk -F\" '/meta/ {if($4=="") $4="dummy"; print $2, $4}' video.smil)
           link1=${links% *}
           link2=${links#* }
           [[ $link2 = "dummy" ]] && unset link2

Open in new window


rez=$(awk -F= '$1 ~ /^RES$/{print $2}' res.cfg)
    case $rez in
        480* )  python $files/SD.py;;
        720* )  python $files/HD.py;;
        1080* ) python $files/FHD.py;;
        * ) echo "Bad response $rez"; exit 1;;
    esac

Open in new window


ty ^^
0
 
peprCommented:
This is unrelated to the original question.
0
 
GaaaraAuthor Commented:
no just  i need help :)  http://www.experts-exchange.com/Programming/Languages/Scripting/Python/Q_28599864.html

i create  a new question ^^ a try  To make him
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 13
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now