In Python there is a value called None, which represents the absence of a value. None is the only value of the NoneType data type. (Other programming languages might call this value null, nil, or undefined.) Just like the Boolean True and False values, None must be typed with a capital N.
This value-without-a-value can be helpful when you need to store something that won’t be confused for a real value in a variable. One place where None is used is as the return value of print(). The print() function displays text on the screen, but it doesn’t need to return anything in the same way len() or input() does. But since all function calls need to evaluate to a return value, print() returns None. To see this in action, enter the following into the interactive shell:
>>> spam = print('Hello!')
Hello!
>>> None == spam
True
Behind the scenes, Python adds return None to the end of any function definition with no return statement. This is similar to how a while or for loop implicitly ends with a continue statement. Also, if you use a return statement without a value (that is, just the return keyword by itself), then None is returned.
安装virtualenv
pip install virtualenv
新建一个目录
E:\>mkdir VEnvDir
E:\>cd VEnvDir
E:\VEnvDir>virtualenv env
Using base prefix 'c:\\python34'
New python executable in E:\VEnvDir\env\Scripts\python.exe
Installing setuptools, pip, wheel...done.
E:\VEnvDir>ls
env
启动虚拟环境
E:\VEnvDir>ls
env
E:\VEnvDir>cd env
E:\VEnvDir\env>ls
Include Lib pip-selfcheck.json Scripts tcl
E:\VEnvDir\env>cd Scripts
E:\VEnvDir\env\Scripts>ls
activate activate.ps1 deactivate.bat easy_install-3.4.exe pip3.4.exe python.exe wheel.exe
activate.bat activate_this.py easy_install.exe pip.exe pip3.exe pythonw.exe
E:\VEnvDir\env\Scripts>activate.bat
(env) E:\VEnvDir\env\Scripts>
退出虚拟环境
(env) E:\VEnvDir\env\Scripts>deactivate.bat
E:\VEnvDir\env\Scripts>
def insertion_sort(collection):
for index in range(1, len(collection)):
while 0 < index and collection[index] < collection[index-1]:
collection[index],collection[index-1] = collection[index-1],collection[index]
index -= 1
return collection
L = [7,1,2,3,9,4,10,5,6,8]
print(insertion_sort(L))
创建目录
E:\ScrapyLego>
创建一个独立的Python运行环境,命名为venv:
E:\ScrapyLego>virtualenv --no-site-packages venv
Using base prefix 'c:\\python34'
New python executable in E:\ScrapyLego\venv\Scripts\python.exe
Installing setuptools, pip, wheel...done.
命令virtualenv就可以创建一个独立的Python运行环境,我们还加上了参数–no-site-packages,这样,已经安装到系统Python环境中的所有第三方包都不会复制过来,这样,我们就得到了一个不带任何第三方包的“干净”的Python运行环境。
进入该环境:
E:\ScrapyLego\venv\Scripts>activate.bat
(venv) E:\ScrapyLego\venv\Scripts>
安装lxml
http://www.lfd.uci.edu/~gohlke/pythonlibs/
(venv) E:\ScrapyLego>pip install lxml-3.7.3-cp34-cp34m-win_amd64.whl
Processing e:\scrapylego\lxml-3.7.3-cp34-cp34m-win_amd64.whl
Installing collected packages: lxml
Successfully installed lxml-3.7.3
lxml-3.7.3-cp34-cp34m-win_amd64.whl 就是lxml的安装包
cp34: python的版本是3.4
安装scrapy
https://docs.scrapy.org/en/latest/intro/install.html
(venv) E:\ScrapyLego>pip install scrapy
新建文件scraper.py
import scrapy
class BrickSetSpider(scrapy.Spider):
name = "brickset_spider"
start_urls = ['http://brickset.com/sets/year-2016']
def saveHtml(self, response):
filename = 'sets2016.html'
with open(filename, 'wb') as f:
f.write(response.body)
def parse(self, response):
self.saveHtml(response)
SET_SELECTOR = '.set'
for brickset in response.css(SET_SELECTOR):
NAME_SELECTOR = 'h1 a ::text'
yield {
#'name': brickset.css(NAME_SELECTOR).extract_first(),
'name': brickset.css(NAME_SELECTOR).extract(),
}
(venv) E:\ScrapyLego>scrapy runspider scraper.py
Tips: ImportError: No module named ‘win32api’
(venv) E:\ScrapyLego>pip install pypiwin32
Collecting pypiwin32
Using cached pypiwin32-219-cp34-none-win_amd64.whl
Installing collected packages: pypiwin32
Successfully installed pypiwin32-219
import scrapy
class BrickSetSpider(scrapy.Spider):
name = "brickset_spider"
start_urls = ['http://brickset.com/sets/year-2016']
def saveHtml(self, response):
filename = 'sets2016.html'
with open(filename, 'wb') as f:
f.write(response.body)
def parse(self, response):
self.saveHtml(response)
SET_SELECTOR = '.set'
for brickset in response.css(SET_SELECTOR):
NAME_SELECTOR = 'h1 a ::text'
PIECES_SELECTOR = './/dl[dt/text() = "Pieces"]/dd/a/text()'
MINIFIGS_SELECTOR = './/dl[dt/text() = "Minifigs"]/dd[2]/a/text()'
IMAGE_SELECTOR = 'img ::attr(src)'
yield {
#'name': brickset.css(NAME_SELECTOR).extract_first(),
'name': brickset.css(NAME_SELECTOR).extract(),
'pieces': brickset.xpath(PIECES_SELECTOR).extract_first(),
'minifigs': brickset.xpath(MINIFIGS_SELECTOR).extract_first(),
'image': brickset.css(IMAGE_SELECTOR).extract_first(),
}
NEXT_PAGE_SELECTOR = '.next a ::attr(href)'
next_page = response.css(NEXT_PAGE_SELECTOR).extract_first()
if next_page:
yield scrapy.Request(
response.urljoin(next_page),
callback = self.parse
)
https://github.com/scrapy/scrapy
https://www.digitalocean.com/community/tutorials/how-to-crawl-a-web-page-with-scrapy-and-python-3
进入目录 C:\Python34\Scripts
进入命令行
执行easy_install.exe pip
然后就安装好了
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('www.sina.com', 80))
s.send(b'GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n')
buffer = []
while True:
d = s.recv(1024)
if d:
buffer.append(d)
else:
break
data = b''.join(buffer)
s.close()
header, html = data.split(b'\r\n\r\n', 1)
print(header.decode('utf-8'))
with open('sina.html', 'wb') as f:
f.write(html)