今天要爬取的是笔画网站的动态图片
网址:http://bishun.strokeorder.info/
打开网页如图
老规矩F12一下看看:
我们想要的是中间这个正在动的“云”字。左上角的小箭头点一下,网页上指哪儿看哪儿。如红框所示,文件就是一个gif的图片。这个爬取就好爬了。
爬取思路
1、selenium库执行自动化打开网页,进行自动化搜索。
2、连接数据库,实现所有文字的循环搜索。
3、使用xpath定位gif文件,获取文件名“title”和图片网页地址“src”。
4、存储文件至指定文件。
按照思路写代码
1,selenium库执行自动化打开网页,进行自动化搜索
导入相关的包
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#创建WebDriver对象
driver = webdriver.Chrome()
#等待变量
driver.get("http://bishun.strokeorder.info/")
#等待“搜索按钮加载完成”
search_btn = driver.find_element_by_xpath("//*[@id='search']/form/input[1]")
#在搜索框输入要查询的字
search_btn.send_keys(wname)
#等待确认按钮加载完成
confirm_btn = wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR,'#search > form > input.button'))
)
#单击“确认”按钮
confirm_btn.click()
#等待5秒
time.sleep(5)
按钮加载,也就是代码中的’#search > form > input.button’是这样获得的。
对于定位到的的字段进行copy,选择copy selector。粘贴过来就是了。
2、连接数据库,实现所有文字的循环搜索
#连接数据库
conn = pymysql.connect("localhost","root","123456","test")
cursor = conn.cursor()
sql = "select wname from test_word"
cursor.execute(sql)
results = cursor.fetchall()
results为所有查询到的内容数组。之后使用for循环遍历数组将遍历内容作为参数传入1中即可完成自动化搜索。
3、使用xpath定位gif文件,获取文件名“title”和图片网页地址“src”
使用xpath必须对html页面进行处理。即记性初始化。不然会报错str/type类型无法使用xpath模块
#获取当前页面的url
url = driver.current_url
#处理html页面
rq = requests.get(url)
rq.emcoding = chardet.detect(rq.content)['encoding']
print(rq.status_code)
html = rq.content.decode('utf-8')
html = etree.HTML(html, parser=etree.HTMLParser(encoding='utf-8'))
#获取文件名和文件链接
PictureList = html.xpath('/html/body/div[1]/img/@src')
Picture = PictureList[0]
NameList = html.xpath('/html/body/div[1]/img/@alt')
Name = NameList[0]
4、存储文件至指定文件
# 设置存储位置
root = r'E:\strokes\\'#指定文件目录
path = root + name + '.gif'
try:
if not os.path.exists(root):
os.mkdir(root) #判断是否有该目录,没有就创建一个
if not os.path.exists(path):
print(url)
r = requests.get(url)
print(r)
with open(path, 'wb') as f:
f.write(r.content) #将文件以二进制的方式存储下来
f.close()
print("文件保存成功")
else:
print("文件已存在")
except:
print("爬取失败")
1、2、3、4进行组合
完整代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import requests
import pymysql
import chardet
from lxml import etree
import os
#创建WebDriver对象
driver = webdriver.Chrome()
#等待变量
wait = WebDriverWait(driver,10)
#连接数据库
conn = pymysql.connect("localhost","root","123456","test")
cursor = conn.cursor()
sql = "select wname from test_word"
cursor.execute(sql)
results = cursor.fetchall()
#自动搜索汉字及页面处理
def Search(wname):
driver.get("http://bishun.strokeorder.info/")
#等待“搜索按钮加载完成”
search_btn = driver.find_element_by_xpath("//*[@id='search']/form/input[1]")#//*[@id="search"]/form/input[1]
#在搜索框输入要查询的字
search_btn.send_keys(wname)
#等待确认按钮加载完成
confirm_btn = wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR,'#search > form > input.button'))##search > form > input.button
)
#单击“确认”按钮
confirm_btn.click()
#等待5秒
time.sleep(5)
#获取当前页面的url
url = driver.current_url
#处理html页面
rq = requests.get(url)
rq.emcoding = chardet.detect(rq.content)['encoding']
print(rq.status_code)
html = rq.content.decode('utf-8')
html = etree.HTML(html, parser=etree.HTMLParser(encoding='utf-8'))
PictureList = html.xpath('/html/body/div[1]/img/@src')
NameList = html.xpath('/html/body/div[1]/img/@alt')
if not PictureList:
return #因为数据库中的文字和爬取网址的库不完全匹配,会出现查询不到的情况。所以加一个判断,如果没有就结束方法。进行下一个字的搜索。
def Saving(url, name):
# 设置存储位置
root = r'E:\strokes\\'
path = root + name + '.gif'
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
print(url)
r = requests.get(url)
print(r)
with open(path, 'wb') as f:
f.write(r.content)
# f = r.replace(r, "1.jpg")
f.close()
print("文件保存成功")
else:
print("文件已存在")
except:
print("爬取失败")
return
def GetPicture():
Picture = PictureList[0]
Name = NameList[0]
Saving(Picture, Name)
return
GetPicture()
return
for row in results:
wname = row[0]
Search(wname)
开始爬爬爬爬爬。。。。。。