python爬虫获取豆瓣网前250部电影的详细信息

news/2024/7/19 11:17:54 标签: 爬虫, python

网址 https://movie.douban.com/top250

一共250部电影,有分页,获取每一部的详细信息

不采用框架,使用 urilib读取网页,re进行正则表达式匹配,lxml进行xpath查找

 1 from film import *
 2 from urllib import request
 3 import time,re
 4 url=r'https://movie.douban.com/top250?start='
 5 for i in range(10):
 6     url=url+str(i*25)
 7     print(url)
 8     
 9     headers = {
10         'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
11                       r'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3',
12         'Connection': 'keep-alive'
13     }
14     req=request.Request(url,headers=headers)
15     page=request.urlopen(req).read()
16     page=page.decode('utf-8')
17     #fp=open("page.txt",mode="w",encoding="UTF-8")
18     #fp.writelines(page)
19     p=re.compile(r'\<em\sclass=\"\"\>\d+\</em\>\s*\<a\shref=\"https://movie.douban.com/subject/\d+/\"\>')
20     result=p.findall(page)
21     for item in result:
22         #print(item)
23         p=re.compile(r'\d+')
24         no=p.findall(item)
25         #print(no[0])
26         p=re.compile(r'https://movie.douban.com/subject/\d+/')
27         rurl=p.findall(item)
28         #print(rurl[0])
29         filma=film(no[0],rurl[0],'','','','','','')
30         filma.getall()
31         filma.detail()
32         time.sleep(3)
33     #print (result)
34     time.sleep(3)
35     #print(i)

 

film.py 如果要做数据的持久化,在这里实现

 1 from urllib import request
 2 from lxml import etree
 3 class film:
 4     def __init__(self,no,url,name,year,score,director,classification,actor):
 5         self.name=name
 6         self.year=year
 7         self.score=score
 8         self.director=director
 9         self.classification=classification
10         self.actor=actor
11         self.url=url
12         self.no=no
13     
14     def detail(self):
15         temp = "No:%s;url:%s;片名:%s;年份:%s;分数:%s;导演:%s;分级:%s;演员:%s;"   %(self.no,self.url,self.name,self.year,self.score,self.director,self.classification,self.actor)  
16         print(temp)
17     def getall(self):
18         headers={
19         'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
20                       r'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3',
21         'Connection': 'keep-alive'
22         }
23         req=request.Request(self.url,headers=headers)
24         page=request.urlopen(req).read()
25         page=page.decode('utf-8')
26         selector=etree.HTML(page)
27         print (page)
28         self.name=selector.xpath('/html/body/div[3]/div[1]/h1/span[1]/text()')
29         self.year=selector.xpath('//*[@id="content"]/h1/span[2]/text()')
30         self.score=selector.xpath('//*[@id="interest_sectl"]/div[1]/div[2]/strong/text()')
31         self.director=selector.xpath('//*[@id="info"]/span[1]/span[2]/a/text()')
32         self.classification=selector.xpath('//*[@id="info"]/span[5]/text()')
33         self.actor=selector.xpath('//*[@id="info"]/span[3]/span[2]/a/text()')
34         
35         

 

转载于:https://www.cnblogs.com/newvoyage/p/7043682.html


http://www.niftyadmin.cn/n/1061388.html

相关文章

【Xamarin开发 Android 系列 3】循序渐进的学习顺序

指定合理的学习步骤&#xff0c;将各个技术点进行强化。慢慢 的就从点到线 到面的飞跃&#xff0c;一切仅仅是时间问题&#xff0c;开始前&#xff0c;请记住&#xff0c;学习是最佳的投资方式。风险成本极小&#xff0c;但是收货极佳。 用最小的杠杠&#xff0c;撬动爆发力极强…

使用全局变量在Activity之间传递数据

DEMO:http://pan.baidu.com/s/1i37naiL 在Activity之间数据传递中还有一种比较实用的方式&#xff0c;就是全局对象&#xff0c;使用J2EE的读者来说都知道Java Web的四个作用域&#xff0c;这四个作用域从小到大分别是&#xff1a;Page、Request、Session和Application&#xf…

js 中的快速排序算法简单实现

对于快速排序&#xff0c;最早是在c中看到&#xff0c;它是利用指针来交换顺序&#xff0c;其实无论哪种语言&#xff0c;原理 和 思想都是一样&#xff0c;然而真正用起来的时候就特别容易忽略一些事实&#xff0c;导致实现失败。废话少说&#xff0c;下面用js实现一下快速排序…

15_四天web内容回顾

1321312转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/7044852.html

Linux的错误码

在使用时需要包含头文件 &#xff03;include <errno.h> merlintfAnalysis:~/projects/tfradius$ cat /usr/include/asm-generic/errno-base.h #ifndef _ASM_GENERIC_ERRNO_BASE_H #define _ASM_GENERIC_ERRNO_BASE_H#define EPERM 1 /* Operation not pe…

《SpringBoot揭秘 快速构建微服务体系》读后感(二)

最简单的springBoot应用 package com.louis.test;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; SpringBootApplication public class Run {public static void main(String[] args) {SpringAppl…

SQL Server truncate与delete的区别

truncate table命令将快速删除数据表中的所有记录&#xff0c;但保留数据表结构。这种快速删除与delete from 数据表的删除全部数据表记录不一样&#xff0c;delete命令删除的数据将存储在系统回滚段中&#xff0c;需要的时候&#xff0c;数据可以回滚恢复&#xff0c;而trunca…

android 实现在文本内容超过固定宽度可手动左右滚动查看效果

实现类似于知乎中的个人签名&#xff0c;如上图中“知识需要分享,教育,健康”可以左右滑动效果。本来以为TextView包含这种属性&#xff0c;但是ellipsize “none”并不能实现这种效果。 所以想到使用EditText 来实现这种效果&#xff1a;<EditTextandroid:id"id/et_te…