具体要感谢大神贡献的代码,大神链接
代码我做了些修改满足我的需求,以下为代码:
# -*- coding: utf-8 -*-
import urllib.request
import json
from pymongo import MongoClient
id = '1761179351'
MONGO_HOST = 'mongodb://localhost:27017/weibo_database' # mongodb host path
proxy_addr = "183.129.207.82:11031"
def use_proxy(url, proxy_addr):
req = urllib.request.Request(url)
req.add_header(
"User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
proxy = urllib.request.ProxyHandler({'http': proxy_addr})
opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
data = urllib.request.urlopen(req).read().decode('utf-8',