เขียน wget พร้อมคุ้กกี้

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.

พอดีนึกครึ้มอยากดูดเว็บมาอ่านเล่นเลยลองเขียนดู

from urllib2 import build_opener,HTTPCookieProcessor
from cookielib import MozillaCookieJar
 
def wget(url,cookie=None):
    c = MozillaCookieJar(cookie)
    if cookie:
        c.load()
 
    fd = build_opener(HTTPCookieProcessor(c)).open(url)
    return fd.read()
 
if __name__ == '__main__':
    print wget('http://www.codenone.com/','cookies.txt')

จริงๆ แล้วก็ไม่มีอะไรมาก แค่เพิ่ม Cookie เข้าไปเท่านั้นเอง ถ้าชอบแบบถึกเถื่อน ก็ต้อง wget ของจริง

import os
 
def wget(url,cookie=None,wget='/usr/bin/wget'):
    args = [wget,'-q','-O','/dev/stdout']
    if cookie:
        args.extend(['--load-cookies','"%s"' % cookie])
    args.append('"%s"' % url)
    cmd = ' '.join(args)
    fd = os.popen(cmd,'r')
    content = fd.read()
    fd.close()
    return content
mk's picture

ถ้าเปลี่ยนเป็น curl แทนล่ะ?

veer's picture

curl -b cookie_file.txt

veer's picture

ที่เคยทำใช้ xmlstarlet + xpath แกะ xhtml (ที่เอา tidy แปลงมาจาก html) ใช้ curl เป็นตัวดูด ต่อๆกันผ่าน shell script :-P

sugree's picture

ปกติใช้ urlopen() กับ re.search()

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น