相关文章推荐
小眼睛的热带鱼  ·  Python equivalent of ...·  2 月前    · 
俊逸的火锅  ·  Python 控制台 — Blender ...·  2 月前    · 
健身的抽屉  ·  在 Google Cloud 上使用 ...·  2 月前    · 
直爽的黑框眼镜  ·  Difference between ...·  2 月前    · 
性感的毛豆  ·  通过SWIG实现在Python中调用C++代 ...·  2 月前    · 
路过的茶叶  ·  传感-驱动一体:具有本体感知功能的电热驱动器 ...·  1 年前    · 
爱喝酒的炒饭  ·  如何在MongoDB中更新具有嵌套数组对象的 ...·  2 年前    · 
还单身的灯泡  ·  Linux下的history命令其实很强大 ...·  2 年前    · 
胡子拉碴的牛肉面  ·  hex转mif文件 verilog - ...·  3 年前    · 
焦虑的针织衫  ·  hls.js - ...·  3 年前    · 
Code  ›  Python equivalent of PHP's file_get_contents on websites (NOT LOCAL FILES) · GitHub
python
https://gist.github.com/TwiN/8e8f928e5b0431e96b60d2caca40556d
小眼睛的热带鱼
2 月前
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Show hidden characters

Updated for Python 3.*:

# some packages were renamed in Python 3
import urllib.request as urllib2
import http.cookiejar as cookielib
def file_get_contents(url):
    url = str(url).replace(" ", "+") # just in case, no space in url
    hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
           'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
           'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
           'Accept-Encoding': 'none',
           'Accept-Language': 'en-US,en;q=0.8',
           'Connection': 'keep-alive'}
    req = urllib2.Request(url, headers=hdr)
        page = urllib2.urlopen(req)
        return page.read()
    except urllib2.HTTPError as e:
        print(e.fp.read())
    return ''
 
推荐文章
小眼睛的热带鱼  ·  Python equivalent of PHP's file_get_contents on websites (NOT LOCAL FILES) · GitHub
2 月前
俊逸的火锅  ·  Python 控制台 — Blender Manual
2 月前
健身的抽屉  ·  在 Google Cloud 上使用 Machine Learning API:实验室挑战赛 | Google Skills
2 月前
直爽的黑框眼镜  ·  Difference between Auto_now_add and Auto_now in Django - GeeksforGeeks
2 月前
性感的毛豆  ·  通过SWIG实现在Python中调用C++代码 – My Home
2 月前
路过的茶叶  ·  传感-驱动一体:具有本体感知功能的电热驱动器_北京理工大学先进结构技术研究院
1 年前
爱喝酒的炒饭  ·  如何在MongoDB中更新具有嵌套数组对象的两个集合的数据?
2 年前
还单身的灯泡  ·  Linux下的history命令其实很强大 - 知乎
2 年前
胡子拉碴的牛肉面  ·  hex转mif文件 verilog - 遥远的路 - 博客园
3 年前
焦虑的针织衫  ·  hls.js - BufferStalledError when i switch the level of the playlist - Stack Overflow
3 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号