参考网址:https://zhuanlan.zhihu.com/p/32037625

def getOneMoviesInfo(Mid,url):
import requests
from lxml import etree #print(url)
data = requests.get(url).text #download the website
s = etree.HTML(data) #analyse data picture = s.xpath('//*[@id="main"]/section/div[1]/div/div/section/div[1]/div[1]/img/@src')
if len(picture)== 0:
picture = 'NULL'
#longPicture = s.xpath('//*[@id="media_v4"]/div[2]/div[1]/div/div/section[3]/div[2]/div/div[1]/img/@src')
name = s.xpath('//*[@id="main"]/section/div[1]/div/div/section/div[2]/section/div[1]/span/a/h2/text()')
if len(name)==0:
print("Mid = %s , failed for a lack of TMDB id "%Mid)
return
name = s.xpath('//*[@id="main"]/section/div[1]/div/div/section/div[2]/section/div[1]/span/a/h2/text()')[0]
year = s.xpath('//*[@id="main"]/section/div[1]/div/div/section/div[2]/section/div[1]/span/span/text()')[0].strip("(").strip().strip(")")
date = s.xpath('//*[@id="media_v4"]/div[2]/div[2]/div/section/div[1]/div/section[1]/ul/li[1]/text()')[1].strip()
brief = s.xpath('//*[@id="main"]/section/div[1]/div/div/section/div[2]/section/div[2]/div/p/text()')[0].replace("\n","\\n") mainCreators =s.xpath('//*[@id="main"]/section/div[1]/div/div/section/div[2]/section/div[2]/ol/li') #all main creators array
writers = []
director = "NULL"
for div in mainCreators:
if len(div.xpath('./p[1]/a/text()'))== 0:
director = 'NULL'
writers = ['NULL','NULL','NULL']
else:
creatorName = div.xpath('./p[1]/a/text()')[0]
#print(creatorName)
creatorProfession = div.xpath('./p[2]/text()')[0]
#print(creatorProfession)
if 'Director' in creatorProfession:
director = creatorName
elif 'Screenplay' in creatorProfession or 'Writer' in creatorProfession:
writers.append(creatorName) stars = []
starsData = s.xpath('//*[@id="media_v4"]/div[2]/div[1]/div/div/section[1]/ol/li')
for div in starsData:
star = div.xpath('./p[1]/a/text()')
if len(star)== 0:
stars == ["NULL","NULL","NULL"]
else:
star = star[0]
stars.append(star) writerslen = len(writers)
starslen=len(stars) for i in range(writerslen,3):
writers.append("NULL");
for i in range(starslen,5):
stars.append("NULL"); with open(r'C:\Users\yuqiao\Desktop\testSpider.txt','a',encoding='utf-8') as f:
f.write("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}\n".format(Mid,name,brief,year,date,director,
writers[0],writers[1],writers[2],
stars[0],stars[1],stars[2],stars[3],stars[4],
picture))
print(Mid)
print(name) #______________________________________________________主函数__________________________________________________________
import time
with open(r'C:\Users\yuqiao\Desktop\testSpider.txt','w',encoding='utf-8') as f:
f.write("")
language = '?language=zh-CN' #######################
with open(r'D:\git\ZiyeMovie\MidURL.txt', "rt",encoding='utf-8') as in_file:
all = in_file.read()
lines = all.split("\n") #for i in range(51,61): 51~60
for i in range(9124,9125):
line = lines[i]
print(line) print('finished')

python 爬虫示例,方便日后参考的更多相关文章

  1. Python爬虫示例

    #!/usr/bin/python #coding:utf8 import re import urllib def gethtml(url): page=urllib.urlopen(url) ht ...

  2. Python爬虫之爬取淘女郎照片示例详解

    这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...

  3. 十个Python爬虫武器库示例,十个爬虫框架,十种实现爬虫的方法!

    一般比价小型的爬虫需求,我是直接使用requests库 + bs4就解决了,再麻烦点就使用selenium解决js的异步 加载问题.相对比较大型的需求才使用框架,主要是便于管理以及扩展等. 1.Scr ...

  4. python爬虫:XPath语法和使用示例

    python爬虫:XPath语法和使用示例 XPath(XML Path Language)是一门在XML文档中查找信息的语言,可以用来在XML文档中对元素和属性进行遍历. 选取节点 XPath使用路 ...

  5. python爬虫之多线程、多进程+代码示例

    python爬虫之多线程.多进程 使用多进程.多线程编写爬虫的代码能有效的提高爬虫爬取目标网站的效率. 一.什么是进程和线程 引用廖雪峰的官方网站关于进程和线程的讲解: 进程:对于操作系统来说,一个任 ...

  6. python爬虫构建代理ip池抓取数据库的示例代码

    爬虫的小伙伴,肯定经常遇到ip被封的情况,而现在网络上的代理ip免费的已经很难找了,那么现在就用python的requests库从爬取代理ip,创建一个ip代理池,以备使用. 本代码包括ip的爬取,检 ...

  7. 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...

  8. [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍

    前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...

  9. [Python爬虫] scrapy爬虫系列 <一>.安装及入门介绍

    前面介绍了很多Selenium基于自动测试的Python爬虫程序,主要利用它的xpath语句,通过分析网页DOM树结构进行爬取内容,同时可以结合Phantomjs模拟浏览器进行鼠标或键盘操作.但是,更 ...

随机推荐

  1. 进击的Python【第十六章】:Web前端基础之jQuery

    进击的Python[第十六章]:Web前端基础之jQuery 一.什么是 jQuery ? jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的少,做的多&quo ...

  2. _bzoj1051 [HAOI2006]受欢迎的牛【强联通】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1051 保存tarjan模版. 求强联通分量,缩点. #include <cstdio& ...

  3. Kali linux 2016.2(Rolling)里的枚举服务

    前言 枚举是一类程序,它允许用户从一个网络中收集某一类的所有相关服务.

  4. Oracle中的序列

    序列是什么? 序列是用来生成唯一.连续的整数的数据库对象.序列通常用来自动生成主键或唯一键的值.序列可以按升序排列,也可以按照降序排列. 其实Oracle中的序列和MySQL中的自增长差不多一个意思. ...

  5. PHP使用Session遇到的一个Permission denied Notice解决办法

    搜索 session.save_path 在这里你有两个选择,一个是像我一样用; 把这一行注释掉,另一个选择就是修改一个 nobody 用户可以操作的目录,也就是说有读写权限的目录,我也查了下这个默认 ...

  6. JS 事件添加onclick写法注意。

    自定义函数添加onclick事件写法注意. 错误写法:element.onclick = addclass(className); 正确写法:element.onclick = function(){ ...

  7. Jboss服务器使用

    一.作者前言 早上坐地铁的时候,阅览about JAVA.了解到一个程序猿,对于服务器的使用,最起码的熟悉那么几种,例如tomcat,jboss,weblogic,websphere,还有Nginx. ...

  8. 短视频SDK用于旅游行业

    超级简单易用的短视频SDK来自RDSDK.COM.锐动天地为开发者提供短视频编辑.视频直播.特效.录屏.编解码.视频转换,等多种解决方案,涵盖PC.iOS.Android多平台.以市场为导向,不断打磨 ...

  9. Visual SVN自动给文件加锁

    在使用SVN作为版本控制器的时候,在VS里安装VISUALSVN插件,当修改文件公共文件的时候需要先Get Lock,如果对于多次操作这个鼠标操作显得是一些复杂,自动给文件加锁的操作实际是给文件加一个 ...

  10. darknet在windows上的安装编译与使用

    darknet(https://github.com/pjreddie/darknet)实现了YOLO网络模型,能快速准确识别多达9000种物体.但其在windows系统上的安装却并非一帆风顺,本文进 ...