2

I am trying store the html code into variable called response using cmdline.execute as shown in below code ,but it is unavailable to store and program code breaks at scrapy shell, can anyone tell me how to store raw html to variable

import scrapy

from scrapy import cmdline

linkedinnurl = "https://stackoverflow.com/users/5597065/adnan-stab=profile"

response = cmdline.execute("scrapy shell https://stackoverflow.com/users/5597065/adnan-s?tab=profile".split()))

print(response)

3
  • Possible duplicate of Saving response from Requests to file Commented May 16, 2019 at 11:55
  • @vezunchik Clearly not a duplicate. The linked question seeks to store the value of requests.post, whereas this question seeks to store the result of an operation initiated by cmdline.execute. Completely different scenario. Commented May 16, 2019 at 11:58
  • Hm, yes, my fault. Thank you. Commented May 16, 2019 at 12:00

1 Answer 1

2

You can do like this to store raw html to variable:

 class MySpider(scrapy.Spider):
        def parse(self, res):
            with open(dynamic_file_name_function(res.url), 'w') as f:
                f.write(res.body)

if you don't need dynamic file name then just do :

 class MySpider(scrapy.Spider):
        def parse(self, res):
            with open(your_file_path, 'w') as f:
                f.write(res.body)
Sign up to request clarification or add additional context in comments.

1 Comment

that was a function to create dynamic filename.You can remove that if you don't need dynamic file name. I have updated the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.