通过python正则表达式获取豆瓣top250的第一页的25个影片排名,影片名字,影片连接,导演,主演,上映日期,国家,剧情,评分,评价人数的内容
网页html内容:
1 <ol class="grid_view"> 2 <li> 3 <div class="item"> 4 <div class="pic"> 5 <em class="">1</em> 6 <a href="https://movie.douban.com/subject/1292052/"> 7 <img width="100" alt="肖申克的救赎" src="https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg" class=""> 8 </a> 9 </div> 10 <div class="info"> 11 <div class="hd"> 12 <a href="https://movie.douban.com/subject/1292052/" class=""> 13 <span class="title">肖申克的救赎</span> 14 <span class="title"> / The Shawshank Redemption</span> 15 <span class="other"> / 月黑高飞(港) / 刺激1995(台)</span> 16 </a> 17 18 19 <span class="playable">[可播放]</span> 20 </div> 21 <div class="bd"> 22 <p class=""> 23 导演: 弗兰克·德拉邦特 Frank Darabont 主演: 蒂姆·罗宾斯 Tim Robbins /...<br> 24 1994 / 美国 / 犯罪 剧情 25 </p> 26 27 28 <div class="star"> 29 <span class="rating5-t"></span> 30 <span class="rating_num" property="v:average">9.6</span> 31 <span property="v:best" content="10.0"></span> 32 <span>1109414人评价</span> 33 </div>
代码:
1 import requests 2 import re 3 url = 'https://movie.douban.com/top250' 4 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'} 5 response = requests.get(url,headers = headers) 6 print('影片排名:',re.findall(re.compile(r'<em class="">(.*)</em>'),response.text)) 7 print('影片名字:',re.findall(re.compile(r'<span class="title">(.*)</span>'),response.text)) 8 print("影片连接:",re.findall(re.compile(r'<a href="(.*)" class=""'),response.text)) 9 print('导演:',re.findall(re.compile(r'导演: (.*) '),response.text)) 10 print('主演:',re.findall(re.compile(r'主演: (.*)<br>'),response.text)) 11 print('上映日期:',re.findall(re.compile(r'(\d\d\d\d) / '),response.text)) 12 print('国家:',re.findall(re.compile(r' / (.*) / '),response.text)) 13 print('剧情',re.findall(re.compile(r' / 剧情 (.*)'),response.text)) 14 print('评分',re.findall(re.compile(r'<span class="rating_num" property="v:average">(.*)</span>'),response.text)) 15 print('评价人数',re.findall(re.compile(r'<span>(.*)人评价'),response.text))
结果: