最近假期有空想学习一下python爬虫,百度一下发现Eclipse可以集成python插件,无奈十分麻烦,IDEA就不同了,引入python 插件十分简单,适合我这个小白
Intellij IDEA15中Python安装:
Intellij IDEA15建立Python项目
编辑Python脚本
在into.py敲写代码
# coding :utf8可读取百度首页的cookie
import urllib
from urllib import request
import http.cookiejar
url = 'http://www.baidu.com'
print('第一种方法')
response1 = request.urlopen(url)
print(response1.getcode())
print(response1.read())
print('第二种方法')
req = request.Request(url)
req.add_header('user-agent','Mozilla/5.0')
response2 = request.urlopen(req)
print(response2.getcode())
print(response2.read())
print('第三种方法')
cj = http.cookiejar.CookieJar()
opener = request.build_opener(request.HTTPCookieProcessor(cj))
request.install_opener(opener)
response3 = request.urlopen(url)
print(response3.getcode())
print(cj)
print(response3.read())
注意:这里是在python3的环境下编写的代码