Import CSV
import requests
import re
headers = {' user-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/59.0.3071.115 safari/537.36 '}
url = ' http://sh.ganji.com/ zpbanyungong/o1/' with
open (' e:/infomation.csv ', ' wt ', encoding= ' UTF8 ', newline= ') as f:
csv_writer = Csv.writer (f, dialect= ' Excel ') #这里打开csv文件开始写入 for
I in range (1,2):
new_url = ' http://sh.ganji.com/ zpbanyungong/o ' + str (i) + '/'
content = Requests.get (new_url,headers=headers). Text Pattern
= Re.compile (' gjalog= "100000002704000200000010@atype=click" > (. *?). *?(.*?) .*? (. *?) ', Re. S) results = Re.findall (pattern,content) for result in Results:name,money,contrict = result name = Re.sub ('/s+ ', ', Name '). Strip () Money = re.sub ('/s+ ', ' ", money). Strip () Contrict = re.sub ('/s+ ', ', ', contrict). Strip () Csv_writer.writerow ([name , Money, Contrict]) # print (name,money,contrict) is the code of this article