用python实现从豆瓣网上爬取王祖贤的照片
安装好python、chromedriver、浏览器的XPath Helper插件 python学习:http://www.runoob.com/python3/python3-tutorial.htmlXPath语法学习:http://www.w3school.com.cn/xpath/xpath_syntax.asp
这次爬取的数据分别为json格式和HTML格式
json格式在www.douban.com网站中,搜索王祖贤,通过开发者模式可以找到请求的地址 https://www.douban.com/j/search_photo?q=%E7%8E%8B%E7%A5%96%E8%B4%A4&limit=20&start=0,如下图所示:
然后我们访问这个url,可以看到返回了json的数据:
# 数据是json格式
def getPhotos():
''' for 循环 请求全部的 url '''
for i in range(0, 200, 20):
url = 'https://www.douban.com/j/search_photo?q=' + query + '&limit=20&start=' + str(i)
html = requests.get(url).text # 得到返回结果
response = json.loads(html, encoding='utf-8') # 将 JSON 格式转换成 Python 对象
for image in response['images']:
print(image['src']) # 查看当前下载的图片网址
download(image['src'], image['id']) # 下载一张图片
HTML格式的访问https://movie.douban.com,搜索 王祖贤,通过查看源码,了解结构,然后用XPath Helper插件做试验,按住ctrl+shift+x 同时鼠标点击王祖贤图片,根据XPath的语法同时根据HTML的结构写解析表达式: //div[@class='item-root']/div[@class='detail']/div[@class='title']/a[@class='title-text']
XPath语法学习:http://www.w3school.com.cn/xpath/xpath_syntax.asp
# 数据是 html格式,有时候网页会用 JS请求数据,只有等JS都加载结束后,才能获取完成的html,但xpath不受限制
def getMoviePhotos():
url = 'https://movie.douban.com/subject_search?search_text=' + query + '&cat=1002'
driver = webdriver.Chrome(chromedriverPath)
driver.get(url)
# 初始化
html = etree.HTML(driver.page_source)
# 使用xpath helper, ctrl+shit+x 选中元素
# xpath 语法 http://www.w3school.com.cn/xpath/xpath_syntax.asp
src_xpath = "//div[@class='item-root']/a[@class='cover-link']/img[@class='cover']/@src"
title_xpath = "//div[@class='item-root']/div[@class='detail']/div[@class='title']/a[@class='title-text']"
srcs = html.xpath(src_xpath)
titles = html.xpath(title_xpath)
# zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,返回由元组组成的对象。优点是节约内存
# 参考 http://www.runoob.com/python3/python3-func-zip.html
for src, title in zip(srcs, titles):
# join 字符串拼接
print('\t'.join([str(src), str(title.text)]))
download(src, title.text)
driver.close()
import requests
import json
# lxml是一个流行的解析库,使用的是Xpath语法,可以解析HTML
from lxml import etree
from selenium import webdriver
query = '王祖贤'
downloadPath = 'D:/workspace/study/python/text_classification/testdata/photos/'
# chromedriver需要配置环境变量,查看网上资料说建议放到python的Scripts目录下
chromedriverPath = 'D:/devsoft/python/Scripts/chromedriver'
''' 下载图片 '''
def download(src, id):
dir = downloadPath + str(id) + '.jpg'
try:
pic = requests.get(src, timeout=10)
fp = open(dir, 'wb')
fp.write(pic.content)
fp.close()
except requests.exceptions.ConnectionError:
print('图片无法下载')
# 数据是json格式
def getPhotos():
''' for 循环 请求全部的 url '''
for i in range(0, 200, 20):
url = 'https://www.douban.com/j/search_photo?q=' + query + '&limit=20&start=' + str(i)
html = requests.get(url).text # 得到返回结果
response = json.loads(html, encoding='utf-8') # 将 JSON 格式转换成 Python 对象
for image in response['images']:
print(image['src']) # 查看当前下载的图片网址
download(image['src'], image['id']) # 下载一张图片
# 数据是 html格式,有时候网页会用 JS请求数据,只有等JS都加载结束后,才能获取完成的html,但xpath不受限制
def getMoviePhotos():
url = 'https://movie.douban.com/subject_search?search_text=' + query + '&cat=1002'
driver = webdriver.Chrome(chromedriverPath)
driver.get(url)
# 初始化
html = etree.HTML(driver.page_source)
# 使用xpath helper, ctrl+shit+x 选中元素
# xpath 语法 http://www.w3school.com.cn/xpath/xpath_syntax.asp
src_xpath = "//div[@class='item-root']/a[@class='cover-link']/img[@class='cover']/@src"
title_xpath = "//div[@class='item-root']/div[@class='detail']/div[@class='title']/a[@class='title-text']"
srcs = html.xpath(src_xpath)
titles = html.xpath(title_xpath)
# zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,返回由元组组成的对象。优点是节约内存
# 参考 http://www.runoob.com/python3/python3-func-zip.html
for src, title in zip(srcs, titles):
# join 字符串拼接
print('\t'.join([str(src), str(title.text)]))
download(src, title.text)
driver.close()
getPhotos()
getMoviePhotos()
用python实现爬虫一直是我想学习的,但却迟迟没有行动,趁着放假,跟着数据分析的专栏做了几个实战的练习,很高兴最后成功了,思路和代码参考了老师的例子和大家的评论,因为我是python零基础,所以通过实战也顺便学了python。整个过程下来的感悟是,还是实战学东西快,以后更要多多实操,而不仅仅看理论。小小的实战成功能激发自己学习的动力,希望大家也能亲自尝试下。