Python practice: get new No Starch Press titles using requests and Beautiful Soup modules

#! usr/bin/env python3
# by lorenzo - 04/10/2017
# get new No Starch Press titles using requests and Beautiful Soup modules
# the code displays each result two times - I'm trying to fix this

import requests, os, bs4, sys

res = requests.get("https://www.nostarch.com")
try:
    res.raise_for_status()
except Exception as exc:
    print("There was a problem: %s" % (exc))

noStarchSoup = bs4.BeautifulSoup(res.text, "html.parser")
titleElems = noStarchSoup.select(".field-items a")
print("Do you want to display the titles (1) or save them in a text file (2)? :")
myChoice = input()
if myChoice == "1":

    if len(titleElems) != 0:
        for i in range(len(titleElems)):
            print(titleElems[i].getText())
    else:
        print("Could not find any title!")

else:
    print("Where do you want to store them (enter valid directory):")
    location = input()
    if not os.path.isdir(location):
        print("Enter a valid directory!")
        sys.exit()
    print("Choose a file name:")
    fileName = input()
    myTitles = open(fileName + ".txt", "w")
    for i in range(len(titleElems)):
        myTitles.write(titleElems[i].getText() + "\n")
    myTitles.close()

Comments about the code are welcome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s