500 Lines or Less | A Web Crawler With asyncio Coroutines:用协程写web爬虫

 1 def fetch(url):
 2     sock = socket.socket()
 3     sock.connect(('xkcd.com', 80))
 4     request = 'GET {} HTTP/1.0\r\nHost: xkcd.com\r\n\r\n'.format(url)
 5     sock.send(request.encode('ascii'))
 6     response = b''
 7     chunk = sock.recv(4096)
 8     while chunk:
 9         response += chunk
10         chunk = sock.recv(4096)
11 
12     # Page is now downloaded.
13     links = parse_links(response)
14     q.add(links)

  

猜你喜欢

转载自www.cnblogs.com/beautycode/p/9814372.html