参考网址: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. java启动参数二

    非标准参数又称为扩展参数,其列表如下: -Xint 设置jvm以解释模式运行,所有的字节码将被直接执行,而不会编译成本地码. -Xbatch 关闭后台代码编译,强制在前台编译,编译完成之后才能进行代码 ...

  2. Toasts官方教程

    Toasts IN THIS DOCUMENT The Basics Positioning your Toast Creating a Custom Toast View 在其它线程中启动Toast ...

  3. Python variable 作用域和初始化

    Python 根据LEGB rule在不同的namespace中找变量 在def的函数中对global 变量做修改还是不推荐的,应该将其作为参数传入函数 try: do_something() cnt ...

  4. Join方法,yield方法,线程的优先级

  5. android开发学习 ------- 【转】Gradle相关

    一直在用AndroidStudio,但是对于其Gradle了解的很少. 推荐 http://www.jianshu.com/p/9df3c3b6067a  觉得说的很棒!

  6. Android开发学习——简单类图

    1.类A继承于类B    (B 是父类,A是 子类) 2.小汽车.自行车 实现车的接口 3.A中有B这个成员变量,单向关联 4.聚合,整体与部分的关系.has-a  B中的构造函数(或set方法)需要 ...

  7. XDocument

    XDocument学习(Winform) using System; using System.Collections.Generic; using System.ComponentModel; us ...

  8. skeljs框架关键点使用

    global  全局 style.css    containers: 1400px;容器宽度 xlarge  超大屏(media: max-width:1680px) style-xlarge.cs ...

  9. Actionscript,AS3,MXML,Flex,Flex Builder,Flash Builder,Flash,AIR,Flash Player之关系

    转自zrong's blog:http://zengrong.net/post/1295.htm ActionScript ActionScript通常简称为AS,它是Flash平台的语言.AS编写的 ...

  10. EL表达式、JSTL

    EL表达式 一.简介 > JSP表达式 <%= %> 用于向页面中输出一个对象.        > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段.   ...