参考网址: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. CMake学习笔记一:初识cmake

    1 cmake简介 1.1 背景知识 cmake 是 kitware 公司以及一些开源开发者在开发几个工具套件(VTK)的过程中衍生品,最终形成体系,成为一个独立的开放源代码项目.项目的诞生时间是 2 ...

  2. [SPOJ375]Qtree

    Description You are given a tree (an acyclic undirected connected graph) with N nodes, and edges num ...

  3. Linux单机环境下HDFS伪分布式集群安装操作步骤v1.0

    公司平台的分布式文件系统基于Hadoop HDFS技术构建,为开发人员学习及后续项目中Hadoop HDFS相关操作提供技术参考特编写此文档.本文档描述了Linux单机环境下Hadoop HDFS伪分 ...

  4. 员工管理系统(集合与IO流的结合使用 beta4.0 ObjectInputStream/ ObjectOutputStream)

    package cn.employee_io; import java.io.Serializable; public class Employee implements Serializable{ ...

  5. jquery readio checked

    今天太鬼火了为这个难问题搜了一下午了到最后还是csdn的朋友给了我正确的答案http://bbs.csdn.net/topics/300162450谢谢这位朋友 // $("#ISOK1&q ...

  6. [BZOJ2005][NOI2010]能量采集 数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 发现与$(0,0)$连线斜率相同的点会被挡住.也就是对于$(a,b)$且$gcd(a ...

  7. No rule to make target ...

    在编译一个Android上的jni的时候出现了如下的问题 make[3]: *** No rule to make target `/home/zhang/android1/src/androidpk ...

  8. VS项目属性配置总结

    以下是针对VS2013下的VC++项目: Debug和Release说明: Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发布版本,它往往是进 ...

  9. jQuery addClass removeClass toggleClass方法概述

    通过动态改变类名(class),可以让其修改元素呈现出不同的效果.在HTML结构中里,多个class以空格分隔,当一个节点(或称为一个标签)含有多个class时,DOM元素响应的className属性 ...

  10. 玩转CPU运行曲线

    Leaf 是不是从来没有想过看看cpu运行曲线啊骚年?顶多也就仅仅是看看cpu利用率,吓自己一跳后感觉关闭几个不该打开的程序~ 然而问题来了,微软公司要让你绘制cpu运行曲线啊!!不仅是固定的直线,还 ...