python 爬虫示例,方便日后参考
参考网址: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 爬虫示例,方便日后参考的更多相关文章
- Python爬虫示例
#!/usr/bin/python #coding:utf8 import re import urllib def gethtml(url): page=urllib.urlopen(url) ht ...
- Python爬虫之爬取淘女郎照片示例详解
这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...
- 十个Python爬虫武器库示例,十个爬虫框架,十种实现爬虫的方法!
一般比价小型的爬虫需求,我是直接使用requests库 + bs4就解决了,再麻烦点就使用selenium解决js的异步 加载问题.相对比较大型的需求才使用框架,主要是便于管理以及扩展等. 1.Scr ...
- python爬虫:XPath语法和使用示例
python爬虫:XPath语法和使用示例 XPath(XML Path Language)是一门在XML文档中查找信息的语言,可以用来在XML文档中对元素和属性进行遍历. 选取节点 XPath使用路 ...
- python爬虫之多线程、多进程+代码示例
python爬虫之多线程.多进程 使用多进程.多线程编写爬虫的代码能有效的提高爬虫爬取目标网站的效率. 一.什么是进程和线程 引用廖雪峰的官方网站关于进程和线程的讲解: 进程:对于操作系统来说,一个任 ...
- python爬虫构建代理ip池抓取数据库的示例代码
爬虫的小伙伴,肯定经常遇到ip被封的情况,而现在网络上的代理ip免费的已经很难找了,那么现在就用python的requests库从爬取代理ip,创建一个ip代理池,以备使用. 本代码包括ip的爬取,检 ...
- 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- [Python爬虫] scrapy爬虫系列 <一>.安装及入门介绍
前面介绍了很多Selenium基于自动测试的Python爬虫程序,主要利用它的xpath语句,通过分析网页DOM树结构进行爬取内容,同时可以结合Phantomjs模拟浏览器进行鼠标或键盘操作.但是,更 ...
随机推荐
- hasLayout原理【转】
项目中经常用到:*html .clearfix{ height:1%;} //IE6能识别*+html .clearfix{height:1%;} //IE7能识别1,有很多方式能触发hasLayo ...
- [转]广义正交匹配追踪(gOMP)
广义正交匹配追踪(Generalized OMP, gOMP)算法可以看作为OMP算法的一种推广,由文献[1]提出,第1作者本硕为哈工大毕业,发表此论文时在Korea University攻读博士学位 ...
- [AHOI2007]密码箱
Description 在一次偶然的情况下,小可可得到了一个密码箱,听说里面藏着一份古代流传下来的藏宝图,只要能破解密码就能打开箱子,而箱子背面刻着的古代图标,就是对密码的提示.经过艰苦的破译,小可可 ...
- Miller&&Pollard HDOJ 4344 Mark the Rope
题目传送门 题意:一个长为n(n<2^63)的管子,在管子上做标记,每隔L个长度单位做一个标记,从管子头端开始,保证最后一次标记恰好在管子的尾端.让你找出有多少个这样的L(L<n),且他们 ...
- 暴力(判凸四边形) FZOJ 2148 Moon Game
题目传送门 题意:给了n个点的坐标,问能有几个凸四边形 分析:数据规模小,直接暴力枚举,每次四个点判断是否会是凹四边形,条件是有一个点在另外三个点的内部,那么问题转换成判断一个点d是否在三角形abc内 ...
- Environment中有大量访问目录的函数
public class Environment { /** * Return root of the "system" partition holding the core An ...
- [转]如何在 TFS 中使用 Git
本文转自 http://www.cnblogs.com/stg609/p/3651688.html 对 Charley Blog 的代码进行版本控制的想法由来已久,在代码建立之初其实已经使用过 TFS ...
- poj3368 Frequent values
思路: 转化为RMQ. 实现: #include <cstdio> #include <cstring> #include <algorithm> using na ...
- Objective -C Categories
Objective -C Categories The dynamic runtime dispatch mechanism employed by Objective-C lets you add ...
- R in action读书笔记(12)第九章 方差分析
第九章方差分析 9.2 ANOVA 模型拟合 9.2.1 aov()函数 aov(formula, data = NULL, projections =FALSE, qr = TRUE, contra ...