python模拟浏览器访问网站

import osimport urllibclass AppURLopener(urllib.FancyURLopener):version"Mozilla5.0"urllib._urlop

import os  
import urllib  
class AppURLopener(urllib.FancyURLopener):  
    version = "Mozilla/5.0"  
urllib._urlopener = AppURLopener()  
url = "http://blog.csdn/eastmount/article/details/44492787"  
content = urllib.urlopen(url).read()
open('test.html','w+').write(content) 


伪装成浏览器访问
某些网站反感爬虫的到访,于是对爬虫一律拒绝请求
这时候我们需要伪装成浏览器,这可以通过修改http包中的header来实现

#…  
  
headers = {  
	'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  
}  
req = urllib2.Request(  
	url = 'http://www.csdn',  
	data = postdata,  
	headers = headers  
)  
#...  
对付"反盗链"
某些站点有所谓的反盗链设置,其实说穿了很简单,

就是检查你发送请求的header里面,referer站点是不是他自己,

所以我们只需要像把headers的referer改成该网站即可,以cnbeta为例:

#...
headers = {
    'Referer':'http://wwwbeta/articles'
}
#...




发布者:admin,转转请注明出处:http://www.yc00.com/web/1743815424a4511802.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信