Have Python Save A Website As XML to Desktop File
I am trying to do something very simple and just can't get it to work. I have spent a few hours on it. Any help is greatly appreciated.
In Firefox, I have saved a "predictit" webpage
(https://www.predictit.org/api/marketdata/markets/4366) as an .xml file on my desktop.
I can successfully use this file as follows:
import xml.etree.ElementTree as ET
tree = ET.parse(r'C:UsersxxxxDesktop4366.xml')
root = tree.getroot()
print(root[5][0][7].text)
My question is how do I get Python to save the website as an xml file on my desktop?
Because I am sure the answer is so simple, I won't document my failures trying requests, urllib, open, write, etc.
Thanks so much for your time and efforts.
Ellie The Good Dog
python xml
add a comment |
I am trying to do something very simple and just can't get it to work. I have spent a few hours on it. Any help is greatly appreciated.
In Firefox, I have saved a "predictit" webpage
(https://www.predictit.org/api/marketdata/markets/4366) as an .xml file on my desktop.
I can successfully use this file as follows:
import xml.etree.ElementTree as ET
tree = ET.parse(r'C:UsersxxxxDesktop4366.xml')
root = tree.getroot()
print(root[5][0][7].text)
My question is how do I get Python to save the website as an xml file on my desktop?
Because I am sure the answer is so simple, I won't document my failures trying requests, urllib, open, write, etc.
Thanks so much for your time and efforts.
Ellie The Good Dog
python xml
add a comment |
I am trying to do something very simple and just can't get it to work. I have spent a few hours on it. Any help is greatly appreciated.
In Firefox, I have saved a "predictit" webpage
(https://www.predictit.org/api/marketdata/markets/4366) as an .xml file on my desktop.
I can successfully use this file as follows:
import xml.etree.ElementTree as ET
tree = ET.parse(r'C:UsersxxxxDesktop4366.xml')
root = tree.getroot()
print(root[5][0][7].text)
My question is how do I get Python to save the website as an xml file on my desktop?
Because I am sure the answer is so simple, I won't document my failures trying requests, urllib, open, write, etc.
Thanks so much for your time and efforts.
Ellie The Good Dog
python xml
I am trying to do something very simple and just can't get it to work. I have spent a few hours on it. Any help is greatly appreciated.
In Firefox, I have saved a "predictit" webpage
(https://www.predictit.org/api/marketdata/markets/4366) as an .xml file on my desktop.
I can successfully use this file as follows:
import xml.etree.ElementTree as ET
tree = ET.parse(r'C:UsersxxxxDesktop4366.xml')
root = tree.getroot()
print(root[5][0][7].text)
My question is how do I get Python to save the website as an xml file on my desktop?
Because I am sure the answer is so simple, I won't document my failures trying requests, urllib, open, write, etc.
Thanks so much for your time and efforts.
Ellie The Good Dog
python xml
python xml
asked Nov 11 '18 at 21:20
Ellie The Good Dog
323
323
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
import requests
url = "https://www.predictit.org/api/marketdata/markets/4366"
res = requests.get(url)
if res.status_code == 200:
with open('filename.xml', 'w') as f:
f.write(res.text)
I think that should do what you want.
Thank you sashaaero. I appreciate it. My problem is then that if I run the code I originally pasted on that Python-saved file...it fails. When I run ET.parse on the file that Python saved, I get: "ParseError: not well-formed (invalid token): line 1, column 0." That is really my problem. Any ideas at that point? Thanks!
– Ellie The Good Dog
Nov 11 '18 at 21:33
@EllieTheGoodDog Yeah, I see. This website returns JSON for this request. Are you okay with JSON or you need XML?
– sashaaero
Nov 11 '18 at 21:41
Hmmm...not sure. XML ideally as I know how to access that. But if that is not possible, I would be fine to try JSON. Thanks so much for your help!
– Ellie The Good Dog
Nov 11 '18 at 21:49
It is possible, but i'm not sure which headers you should set. Default doesn't work for me. Also work with JSON is easy. You can justdata = json.loads(text)
and you have a dictionary of given data.
– sashaaero
Nov 11 '18 at 21:51
Got it - I saw another posts that explains json.loads. I did not recognize that they curly brackets was json. Thanks for pointing that out! I should be able to figure it out from here. Thanks so much.
– Ellie The Good Dog
Nov 11 '18 at 21:52
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%2f53253342%2fhave-python-save-a-website-as-xml-to-desktop-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
import requests
url = "https://www.predictit.org/api/marketdata/markets/4366"
res = requests.get(url)
if res.status_code == 200:
with open('filename.xml', 'w') as f:
f.write(res.text)
I think that should do what you want.
Thank you sashaaero. I appreciate it. My problem is then that if I run the code I originally pasted on that Python-saved file...it fails. When I run ET.parse on the file that Python saved, I get: "ParseError: not well-formed (invalid token): line 1, column 0." That is really my problem. Any ideas at that point? Thanks!
– Ellie The Good Dog
Nov 11 '18 at 21:33
@EllieTheGoodDog Yeah, I see. This website returns JSON for this request. Are you okay with JSON or you need XML?
– sashaaero
Nov 11 '18 at 21:41
Hmmm...not sure. XML ideally as I know how to access that. But if that is not possible, I would be fine to try JSON. Thanks so much for your help!
– Ellie The Good Dog
Nov 11 '18 at 21:49
It is possible, but i'm not sure which headers you should set. Default doesn't work for me. Also work with JSON is easy. You can justdata = json.loads(text)
and you have a dictionary of given data.
– sashaaero
Nov 11 '18 at 21:51
Got it - I saw another posts that explains json.loads. I did not recognize that they curly brackets was json. Thanks for pointing that out! I should be able to figure it out from here. Thanks so much.
– Ellie The Good Dog
Nov 11 '18 at 21:52
add a comment |
import requests
url = "https://www.predictit.org/api/marketdata/markets/4366"
res = requests.get(url)
if res.status_code == 200:
with open('filename.xml', 'w') as f:
f.write(res.text)
I think that should do what you want.
Thank you sashaaero. I appreciate it. My problem is then that if I run the code I originally pasted on that Python-saved file...it fails. When I run ET.parse on the file that Python saved, I get: "ParseError: not well-formed (invalid token): line 1, column 0." That is really my problem. Any ideas at that point? Thanks!
– Ellie The Good Dog
Nov 11 '18 at 21:33
@EllieTheGoodDog Yeah, I see. This website returns JSON for this request. Are you okay with JSON or you need XML?
– sashaaero
Nov 11 '18 at 21:41
Hmmm...not sure. XML ideally as I know how to access that. But if that is not possible, I would be fine to try JSON. Thanks so much for your help!
– Ellie The Good Dog
Nov 11 '18 at 21:49
It is possible, but i'm not sure which headers you should set. Default doesn't work for me. Also work with JSON is easy. You can justdata = json.loads(text)
and you have a dictionary of given data.
– sashaaero
Nov 11 '18 at 21:51
Got it - I saw another posts that explains json.loads. I did not recognize that they curly brackets was json. Thanks for pointing that out! I should be able to figure it out from here. Thanks so much.
– Ellie The Good Dog
Nov 11 '18 at 21:52
add a comment |
import requests
url = "https://www.predictit.org/api/marketdata/markets/4366"
res = requests.get(url)
if res.status_code == 200:
with open('filename.xml', 'w') as f:
f.write(res.text)
I think that should do what you want.
import requests
url = "https://www.predictit.org/api/marketdata/markets/4366"
res = requests.get(url)
if res.status_code == 200:
with open('filename.xml', 'w') as f:
f.write(res.text)
I think that should do what you want.
answered Nov 11 '18 at 21:27
sashaaero
8331720
8331720
Thank you sashaaero. I appreciate it. My problem is then that if I run the code I originally pasted on that Python-saved file...it fails. When I run ET.parse on the file that Python saved, I get: "ParseError: not well-formed (invalid token): line 1, column 0." That is really my problem. Any ideas at that point? Thanks!
– Ellie The Good Dog
Nov 11 '18 at 21:33
@EllieTheGoodDog Yeah, I see. This website returns JSON for this request. Are you okay with JSON or you need XML?
– sashaaero
Nov 11 '18 at 21:41
Hmmm...not sure. XML ideally as I know how to access that. But if that is not possible, I would be fine to try JSON. Thanks so much for your help!
– Ellie The Good Dog
Nov 11 '18 at 21:49
It is possible, but i'm not sure which headers you should set. Default doesn't work for me. Also work with JSON is easy. You can justdata = json.loads(text)
and you have a dictionary of given data.
– sashaaero
Nov 11 '18 at 21:51
Got it - I saw another posts that explains json.loads. I did not recognize that they curly brackets was json. Thanks for pointing that out! I should be able to figure it out from here. Thanks so much.
– Ellie The Good Dog
Nov 11 '18 at 21:52
add a comment |
Thank you sashaaero. I appreciate it. My problem is then that if I run the code I originally pasted on that Python-saved file...it fails. When I run ET.parse on the file that Python saved, I get: "ParseError: not well-formed (invalid token): line 1, column 0." That is really my problem. Any ideas at that point? Thanks!
– Ellie The Good Dog
Nov 11 '18 at 21:33
@EllieTheGoodDog Yeah, I see. This website returns JSON for this request. Are you okay with JSON or you need XML?
– sashaaero
Nov 11 '18 at 21:41
Hmmm...not sure. XML ideally as I know how to access that. But if that is not possible, I would be fine to try JSON. Thanks so much for your help!
– Ellie The Good Dog
Nov 11 '18 at 21:49
It is possible, but i'm not sure which headers you should set. Default doesn't work for me. Also work with JSON is easy. You can justdata = json.loads(text)
and you have a dictionary of given data.
– sashaaero
Nov 11 '18 at 21:51
Got it - I saw another posts that explains json.loads. I did not recognize that they curly brackets was json. Thanks for pointing that out! I should be able to figure it out from here. Thanks so much.
– Ellie The Good Dog
Nov 11 '18 at 21:52
Thank you sashaaero. I appreciate it. My problem is then that if I run the code I originally pasted on that Python-saved file...it fails. When I run ET.parse on the file that Python saved, I get: "ParseError: not well-formed (invalid token): line 1, column 0." That is really my problem. Any ideas at that point? Thanks!
– Ellie The Good Dog
Nov 11 '18 at 21:33
Thank you sashaaero. I appreciate it. My problem is then that if I run the code I originally pasted on that Python-saved file...it fails. When I run ET.parse on the file that Python saved, I get: "ParseError: not well-formed (invalid token): line 1, column 0." That is really my problem. Any ideas at that point? Thanks!
– Ellie The Good Dog
Nov 11 '18 at 21:33
@EllieTheGoodDog Yeah, I see. This website returns JSON for this request. Are you okay with JSON or you need XML?
– sashaaero
Nov 11 '18 at 21:41
@EllieTheGoodDog Yeah, I see. This website returns JSON for this request. Are you okay with JSON or you need XML?
– sashaaero
Nov 11 '18 at 21:41
Hmmm...not sure. XML ideally as I know how to access that. But if that is not possible, I would be fine to try JSON. Thanks so much for your help!
– Ellie The Good Dog
Nov 11 '18 at 21:49
Hmmm...not sure. XML ideally as I know how to access that. But if that is not possible, I would be fine to try JSON. Thanks so much for your help!
– Ellie The Good Dog
Nov 11 '18 at 21:49
It is possible, but i'm not sure which headers you should set. Default doesn't work for me. Also work with JSON is easy. You can just
data = json.loads(text)
and you have a dictionary of given data.– sashaaero
Nov 11 '18 at 21:51
It is possible, but i'm not sure which headers you should set. Default doesn't work for me. Also work with JSON is easy. You can just
data = json.loads(text)
and you have a dictionary of given data.– sashaaero
Nov 11 '18 at 21:51
Got it - I saw another posts that explains json.loads. I did not recognize that they curly brackets was json. Thanks for pointing that out! I should be able to figure it out from here. Thanks so much.
– Ellie The Good Dog
Nov 11 '18 at 21:52
Got it - I saw another posts that explains json.loads. I did not recognize that they curly brackets was json. Thanks for pointing that out! I should be able to figure it out from here. Thanks so much.
– Ellie The Good Dog
Nov 11 '18 at 21:52
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%2f53253342%2fhave-python-save-a-website-as-xml-to-desktop-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