Home > other >  Small white to ask how to set up a multi-threaded run, climb is interrupted my code
Small white to ask how to set up a multi-threaded run, climb is interrupted my code

Time:11-23

 # - * - coding: utf-8 - * - 



The from LXML import etree
The import requests
The import CSV
The import time

Def writecsv (item) :
With the open (' ershou. CSV ', 'a', encoding="utf-8") as f: # CSV file name
Writer.=the CSV writer (f)
Try:
Writer. Writerow (item)
Except:
Print (' write error! ')


If __name__=="__main__ ':
Headers={' the user-agent ':' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.3 '}

Start_url='https://cd.ke.com/ershoufang/damian/pg'
For x in range (1,76) :
Url=start_url + STR (x)
HTML=requests. Get (url, headers=headers)
Time. Sleep (3)
The selector=etree. HTML (HTML. Text)


Xiaoqulist=selector. Xpath ('//* [@ id="beike"]/div [1]/div [4]/div [1]/div [4]/ul/li ')
For xiaoqu in xiaoqulist:

# building name
Mingcheng=xiaoqu. Xpath (' div/div [2]/div [1]/div/a/text () ') [0]

# building information
Xinxi=xiaoqu. Xpath (' div/div [2]/div [2]/text () ') [1]

# average
Junjia=xiaoqu. Xpath (' div/div [2]/div [5]/div [2]/span/text () ') [0]

# price
Zongjia=xiaoqu. Xpath ('//div/div [5]/div/@/span/text () ') [0]


Item=[mingcheng, xinxi, junjia, zongjia]
Writecsv (item)
Print (' londing: ', mingcheng)


CodePudding user response:

 
The from multiprocessing import Pool
Def main (num1) :
Pass
Print (" over a ")
If __name__=="__main__ ':
Pool=pool (8) # number is the number of process need to, as long as you are memory and CPU are strong enough,
# pool. The map (the main, a list) is the main process, pay attention to do not add parentheses, the list is an iterative object, the inside of the element is the main parameters () need
Pool. The start () # multi-process
Pool. The join () # of all the main process for the child, to the end of the
Print (" complete ")

When I opened the 50 process, to take up my 40 gb of memory,

CodePudding user response:

With scrapy
  • Related