python not writing to file.
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
296
296
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
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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,11131226
2,11131226
add a comment |
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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
6,84422233
6,84422233
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