2

I tried to write a program that can automatically down files (with php links). However, I have two issue right now

First, my target website requires registration for the first time access. Then, every time when I clicked the download link, it automatically downloads the file I want. It looks like searched some cookies that saved on my computer to determine who I am. How to make my python program deal with my local cookies? if multiples?

Second, can anyone provide me an example code about how to deal with php download link file? I want to save all these files on a specific location with a specific name. How should I do that in python3?

yorkevin
  • 21
  • 2

1 Answers1

0

For getting the cookies:

Try:

import urllib.request
cookier = urllib.request.HTTPCookieProcessor()
# create the cookie handler
opener = urllib.request.build_opener(cookier)
urllib.request.install_opener(opener)

The HTTPCookieProcessor will return cookielib.CookieJar object which contains those cookies. You can loop through it to find the cookie you want.

for c in cookier.cookiejar: 
    if c.domain == '.stackoverflow.com': 
        # do something

For read the content in the link:

Try:

url = 'YOUR_URL'
req = urllib.request.Request(url, headers=_headers) # where headers is the header setting you can find in your brwoser
f = urllib.request.urlopen(req)
contents = f.read().decode('utf-8')
# contents is the content inside your file
# You can add the code here to write contents to other file to save it
R.yan
  • 2,214
  • 1
  • 16
  • 33
  • Thanks a lot. But I don't really understand the cookie part. how should I use this cookier? and also, what's the mechanism behind this? Can you explain this to me a bit? – yorkevin May 02 '18 at 06:46
  • `for c in cookier.cookiejar: if c.domain == '.yahoo.com': # do something ` You can do something like this. – R.yan May 02 '18 at 06:48
  • And once you find the cookie you want, you can call `c.value` to get it back and use it somewhere – R.yan May 02 '18 at 06:50
  • @yorkevin can you get the cookie now? – R.yan May 06 '18 at 09:09