python not writing to file.












0














from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request("https://www.youtube.com/watch?v=YBn0TxzmKXI")
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")

tags = soup.find_all('a')

for tag in tags:
t = tag.get('href')
x = t.find('watch?v')
if x > 0:
with open("C:BGOutput.txt", "a+") as text_file:
text_file.write("Links are :: " % x)


I am trying to write to file called output.txt rather than print on screen.
Also I want to skip writing to file if contain text "google"



How can I do that
But this code is not doing it










share|improve this question






















  • You need to escape your backslashes in your file path i.e. "C:\BG\Output.txt" or use a raw string i.e. r"C:BGOutput.txt".
    – Idlehands
    Nov 11 at 3:29
















0














from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request("https://www.youtube.com/watch?v=YBn0TxzmKXI")
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")

tags = soup.find_all('a')

for tag in tags:
t = tag.get('href')
x = t.find('watch?v')
if x > 0:
with open("C:BGOutput.txt", "a+") as text_file:
text_file.write("Links are :: " % x)


I am trying to write to file called output.txt rather than print on screen.
Also I want to skip writing to file if contain text "google"



How can I do that
But this code is not doing it










share|improve this question






















  • You need to escape your backslashes in your file path i.e. "C:\BG\Output.txt" or use a raw string i.e. r"C:BGOutput.txt".
    – Idlehands
    Nov 11 at 3:29














0












0








0







from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request("https://www.youtube.com/watch?v=YBn0TxzmKXI")
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")

tags = soup.find_all('a')

for tag in tags:
t = tag.get('href')
x = t.find('watch?v')
if x > 0:
with open("C:BGOutput.txt", "a+") as text_file:
text_file.write("Links are :: " % x)


I am trying to write to file called output.txt rather than print on screen.
Also I want to skip writing to file if contain text "google"



How can I do that
But this code is not doing it










share|improve this question













from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request("https://www.youtube.com/watch?v=YBn0TxzmKXI")
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")

tags = soup.find_all('a')

for tag in tags:
t = tag.get('href')
x = t.find('watch?v')
if x > 0:
with open("C:BGOutput.txt", "a+") as text_file:
text_file.write("Links are :: " % x)


I am trying to write to file called output.txt rather than print on screen.
Also I want to skip writing to file if contain text "google"



How can I do that
But this code is not doing it







python-3.x beautifulsoup






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 2:50









NewtoPython

347




347












  • You need to escape your backslashes in your file path i.e. "C:\BG\Output.txt" or use a raw string i.e. r"C:BGOutput.txt".
    – Idlehands
    Nov 11 at 3:29


















  • You need to escape your backslashes in your file path i.e. "C:\BG\Output.txt" or use a raw string i.e. r"C:BGOutput.txt".
    – Idlehands
    Nov 11 at 3:29
















You need to escape your backslashes in your file path i.e. "C:\BG\Output.txt" or use a raw string i.e. r"C:BGOutput.txt".
– Idlehands
Nov 11 at 3:29




You need to escape your backslashes in your file path i.e. "C:\BG\Output.txt" or use a raw string i.e. r"C:BGOutput.txt".
– Idlehands
Nov 11 at 3:29












3 Answers
3






active

oldest

votes


















0














In regards to program not writing to file



It looks like there is an issue with your code indentation. If you move the line text_file.write("Links are :: " % x) one indent level futher to the right, it may solve your problem.



In regards to checking links the link to google



You might try using String.index() (linked here) to see if you can find an occurrence of 'google.com'.






share|improve this answer































    0














    if 'watch?v' in t and 'google' not in t:
    with open("Output.txt", "a+") as text_file:
    text_file.write("Links are :: " + t)
    text_file.write('n')


    Its simple text in string gives match text not in works for tag not having google



    Output



    Links are :: /watch?v=rb8K4nv2y7A
    Links are :: /watch?v=rb8K4nv2y7A
    .
    .





    share|improve this answer





























      0














      you have two error here:



      text_file.write("Links are :: " % x)


      first no %s where variables should be inserted, second x is index it should be t.



      for performance it better to open file outside loop



      with open("C:BGOutput.txt", "a+") as text_file:
      for tag in tags:
      t = tag.get('href')
      x = t.find('watch?v')
      # if 'watch?v' in t:
      # or
      if x > 0:
      text_file.write("Links are :: %sn" % t)
      # or
      # text_file.write("Links are :: " + t + "n")





      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245430%2fpython-not-writing-to-file%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        In regards to program not writing to file



        It looks like there is an issue with your code indentation. If you move the line text_file.write("Links are :: " % x) one indent level futher to the right, it may solve your problem.



        In regards to checking links the link to google



        You might try using String.index() (linked here) to see if you can find an occurrence of 'google.com'.






        share|improve this answer




























          0














          In regards to program not writing to file



          It looks like there is an issue with your code indentation. If you move the line text_file.write("Links are :: " % x) one indent level futher to the right, it may solve your problem.



          In regards to checking links the link to google



          You might try using String.index() (linked here) to see if you can find an occurrence of 'google.com'.






          share|improve this answer


























            0












            0








            0






            In regards to program not writing to file



            It looks like there is an issue with your code indentation. If you move the line text_file.write("Links are :: " % x) one indent level futher to the right, it may solve your problem.



            In regards to checking links the link to google



            You might try using String.index() (linked here) to see if you can find an occurrence of 'google.com'.






            share|improve this answer














            In regards to program not writing to file



            It looks like there is an issue with your code indentation. If you move the line text_file.write("Links are :: " % x) one indent level futher to the right, it may solve your problem.



            In regards to checking links the link to google



            You might try using String.index() (linked here) to see if you can find an occurrence of 'google.com'.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 3:05

























            answered Nov 11 at 2:56









            Lukey McPoopy

            12




            12

























                0














                if 'watch?v' in t and 'google' not in t:
                with open("Output.txt", "a+") as text_file:
                text_file.write("Links are :: " + t)
                text_file.write('n')


                Its simple text in string gives match text not in works for tag not having google



                Output



                Links are :: /watch?v=rb8K4nv2y7A
                Links are :: /watch?v=rb8K4nv2y7A
                .
                .





                share|improve this answer


























                  0














                  if 'watch?v' in t and 'google' not in t:
                  with open("Output.txt", "a+") as text_file:
                  text_file.write("Links are :: " + t)
                  text_file.write('n')


                  Its simple text in string gives match text not in works for tag not having google



                  Output



                  Links are :: /watch?v=rb8K4nv2y7A
                  Links are :: /watch?v=rb8K4nv2y7A
                  .
                  .





                  share|improve this answer
























                    0












                    0








                    0






                    if 'watch?v' in t and 'google' not in t:
                    with open("Output.txt", "a+") as text_file:
                    text_file.write("Links are :: " + t)
                    text_file.write('n')


                    Its simple text in string gives match text not in works for tag not having google



                    Output



                    Links are :: /watch?v=rb8K4nv2y7A
                    Links are :: /watch?v=rb8K4nv2y7A
                    .
                    .





                    share|improve this answer












                    if 'watch?v' in t and 'google' not in t:
                    with open("Output.txt", "a+") as text_file:
                    text_file.write("Links are :: " + t)
                    text_file.write('n')


                    Its simple text in string gives match text not in works for tag not having google



                    Output



                    Links are :: /watch?v=rb8K4nv2y7A
                    Links are :: /watch?v=rb8K4nv2y7A
                    .
                    .






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 11 at 3:37









                    Prateek

                    2,18731226




                    2,18731226























                        0














                        you have two error here:



                        text_file.write("Links are :: " % x)


                        first no %s where variables should be inserted, second x is index it should be t.



                        for performance it better to open file outside loop



                        with open("C:BGOutput.txt", "a+") as text_file:
                        for tag in tags:
                        t = tag.get('href')
                        x = t.find('watch?v')
                        # if 'watch?v' in t:
                        # or
                        if x > 0:
                        text_file.write("Links are :: %sn" % t)
                        # or
                        # text_file.write("Links are :: " + t + "n")





                        share|improve this answer


























                          0














                          you have two error here:



                          text_file.write("Links are :: " % x)


                          first no %s where variables should be inserted, second x is index it should be t.



                          for performance it better to open file outside loop



                          with open("C:BGOutput.txt", "a+") as text_file:
                          for tag in tags:
                          t = tag.get('href')
                          x = t.find('watch?v')
                          # if 'watch?v' in t:
                          # or
                          if x > 0:
                          text_file.write("Links are :: %sn" % t)
                          # or
                          # text_file.write("Links are :: " + t + "n")





                          share|improve this answer
























                            0












                            0








                            0






                            you have two error here:



                            text_file.write("Links are :: " % x)


                            first no %s where variables should be inserted, second x is index it should be t.



                            for performance it better to open file outside loop



                            with open("C:BGOutput.txt", "a+") as text_file:
                            for tag in tags:
                            t = tag.get('href')
                            x = t.find('watch?v')
                            # if 'watch?v' in t:
                            # or
                            if x > 0:
                            text_file.write("Links are :: %sn" % t)
                            # or
                            # text_file.write("Links are :: " + t + "n")





                            share|improve this answer












                            you have two error here:



                            text_file.write("Links are :: " % x)


                            first no %s where variables should be inserted, second x is index it should be t.



                            for performance it better to open file outside loop



                            with open("C:BGOutput.txt", "a+") as text_file:
                            for tag in tags:
                            t = tag.get('href')
                            x = t.find('watch?v')
                            # if 'watch?v' in t:
                            # or
                            if x > 0:
                            text_file.write("Links are :: %sn" % t)
                            # or
                            # text_file.write("Links are :: " + t + "n")






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 11 at 10:39









                            ewwink

                            9,45422236




                            9,45422236






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245430%2fpython-not-writing-to-file%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Tangent Lines Diagram Along Smooth Curve

                                Yusuf al-Mu'taman ibn Hud

                                Zucchini