爬取bean half Top250
作者:互联网
1 import re 2 import requests 3 import csv 4 5 url = 'https://movie.douban.com/top250' 6 7 header = { 8 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " 9 "Chrome/60.0.3100.0 Safari/537.36 " 10 } 11 12 index = 0 13 while index < 10: 14 param = { 15 "start": index * 25 16 } 17 res = requests.get(url, headers=header, params=param) 18 pageContent = res.text 19 20 # 正则表达式解析数据 21 obj = re.compile(r'<li>.*?<span class="title">(?P<name>.*?)' 22 r'</span>.*?<p class="">.*?<br>(?P<year>.*?) / ' 23 r'(?P<place>.*?) .*?</p>', re.S) 24 25 result = obj.finditer(pageContent) 26 27 f = open('csv/top250.csv', mode='a+', encoding='utf-8') 28 csvWriter = csv.writer(f) 29 num = 1 30 for i in result: 31 dic = i.groupdict() 32 dic['year'] = dic['year'].strip() 33 print(dic.values()) 34 csvWriter.writerow(dic.values()) 35 num = num+1 36 f.close() 37 index = index + 1
标签:index,dic,爬取,re,bean,num,nbsp,csv,Top250 来源: https://www.cnblogs.com/ang0/p/16325178.html