python not writing to file.
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
add a comment |
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
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
add a comment |
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
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
python-3.x beautifulsoup
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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'
.
add a comment |
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
.
.
add a comment |
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")
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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'
.
add a comment |
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'
.
add a comment |
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'
.
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'
.
edited Nov 11 at 3:05
answered Nov 11 at 2:56
Lukey McPoopy
12
12
add a comment |
add a comment |
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
.
.
add a comment |
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
.
.
add a comment |
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
.
.
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
.
.
answered Nov 11 at 3:37
Prateek
2,18731226
2,18731226
add a comment |
add a comment |
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")
add a comment |
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")
add a comment |
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")
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")
answered Nov 11 at 10:39
ewwink
9,45422236
9,45422236
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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