1- ## 百度指数抓取,再用图像识别得到指数
2- ### 安装的库很多:
3- > 谷歌图像识别tesseract-ocr
1+ ## 百度指数抓取,再用图像识别得到指数
2+
3+ ## 前言:
4+ 土福曾说,百度指数很难抓,在淘宝上面是20块1个关键字:
5+ ![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110162432795-1923984431.png )
6+
7+ ### 安装的库很多:
8+ > 谷歌图像识别tesseract-ocr
49
510> pip3 install pillow
611
1217
1318> chromedriver.exe
1419
15- ### 图像识别验证码请参考我的博客:
16- [ python图像识别--验证码] ( http://www.cnblogs.com/TTyb/p/5996847.html )
20+ ### 图像识别验证码请参考我的博客:
21+ [ python图像识别--验证码] ( http://www.cnblogs.com/TTyb/p/5996847.html )
1722
18- ### selenium用法请参考我的博客:
19- [ python之selenium] ( http://www.cnblogs.com/TTyb/p/5842015.html )
23+ ### selenium用法请参考我的博客:
24+ [ python之selenium] ( http://www.cnblogs.com/TTyb/p/5842015.html )
2025
21- ### 进入百度指数需要登陆,登陆的账号密码写在文本account里面:
26+ ### 进入百度指数需要登陆,登陆的账号密码写在文本account里面:
2227![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110153714827-1835068903.png )
2328
24- ### 万能登陆代码如下:
29+ ### 万能登陆代码如下:
2530```
26- # 打开浏览器
31+ # 打开浏览器
2732def openbrowser():
2833 global browser
2934
3035 # https://passport.baidu.com/v2/?login
3136 url = "https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F"
32- # 打开谷歌浏览器
37+ # 打开谷歌浏览器
3338 # Firefox()
3439 # Chrome()
3540 browser = webdriver.Chrome()
36- # 输入网址
41+ # 输入网址
3742 browser.get(url)
38- # 打开浏览器时间
39- # print("等待10秒打开浏览器...")
43+ # 打开浏览器时间
44+ # print("等待10秒打开浏览器...")
4045 # time.sleep(10)
4146
42- # 找到id="TANGRAM__PSP_3__userName"的对话框
43- # 清空输入框
47+ # 找到id="TANGRAM__PSP_3__userName"的对话框
48+ # 清空输入框
4449 browser.find_element_by_id("TANGRAM__PSP_3__userName").clear()
4550 browser.find_element_by_id("TANGRAM__PSP_3__password").clear()
4651
47- # 输入账号密码
48- # 输入账号密码
52+ # 输入账号密码
53+ # 输入账号密码
4954 account = []
5055 try:
5156 fileaccount = open("../baidu/account.txt")
@@ -55,40 +60,40 @@ def openbrowser():
5560 fileaccount.close()
5661 except Exception as err:
5762 print(err)
58- input("请正确在account.txt里面写入账号密码")
63+ input("请正确在account.txt里面写入账号密码")
5964 exit()
6065 browser.find_element_by_id("TANGRAM__PSP_3__userName").send_keys(account[0])
6166 browser.find_element_by_id("TANGRAM__PSP_3__password").send_keys(account[1])
6267
63- # 点击登陆登陆
68+ # 点击登陆登陆
6469 # id="TANGRAM__PSP_3__submit"
6570 browser.find_element_by_id("TANGRAM__PSP_3__submit").click()
6671
67- # 等待登陆10秒
68- # print('等待登陆10秒...')
72+ # 等待登陆10秒
73+ # print('等待登陆10秒...')
6974 # time.sleep(10)
70- print("等待网址加载完毕...")
75+ print("等待网址加载完毕...")
7176
72- select = input("请观察浏览器网站是否已经登陆(y/n):")
77+ select = input("请观察浏览器网站是否已经登陆(y/n):")
7378 while 1:
7479 if select == "y" or select == "Y":
75- print("登陆成功!")
76- print("准备打开新的窗口...")
80+ print("登陆成功!")
81+ print("准备打开新的窗口...")
7782 # time.sleep(1)
7883 # browser.quit()
7984 break
8085
8186 elif select == "n" or select == "N":
82- selectno = input("账号密码错误请按0,验证码出现请按1...")
83- # 账号密码错误则重新输入
87+ selectno = input("账号密码错误请按0,验证码出现请按1...")
88+ # 账号密码错误则重新输入
8489 if selectno == "0":
8590
86- # 找到id="TANGRAM__PSP_3__userName"的对话框
87- # 清空输入框
91+ # 找到id="TANGRAM__PSP_3__userName"的对话框
92+ # 清空输入框
8893 browser.find_element_by_id("TANGRAM__PSP_3__userName").clear()
8994 browser.find_element_by_id("TANGRAM__PSP_3__password").clear()
9095
91- # 输入账号密码
96+ # 输入账号密码
9297 account = []
9398 try:
9499 fileaccount = open("../baidu/account.txt")
@@ -98,57 +103,57 @@ def openbrowser():
98103 fileaccount.close()
99104 except Exception as err:
100105 print(err)
101- input("请正确在account.txt里面写入账号密码")
106+ input("请正确在account.txt里面写入账号密码")
102107 exit()
103108
104109 browser.find_element_by_id("TANGRAM__PSP_3__userName").send_keys(account[0])
105110 browser.find_element_by_id("TANGRAM__PSP_3__password").send_keys(account[1])
106- # 点击登陆sign in
111+ # 点击登陆sign in
107112 # id="TANGRAM__PSP_3__submit"
108113 browser.find_element_by_id("TANGRAM__PSP_3__submit").click()
109114
110115 elif selectno == "1":
111- # 验证码的id为id="ap_captcha_guess"的对话框
112- input("请在浏览器中输入验证码并登陆...")
113- select = input("请观察浏览器网站是否已经登陆(y/n):")
116+ # 验证码的id为id="ap_captcha_guess"的对话框
117+ input("请在浏览器中输入验证码并登陆...")
118+ select = input("请观察浏览器网站是否已经登陆(y/n):")
114119
115120 else:
116- print("请输入“y”或者“n”!")
117- select = input("请观察浏览器网站是否已经登陆(y/n):")
121+ print("请输入“y”或者“n”!")
122+ select = input("请观察浏览器网站是否已经登陆(y/n):")
118123```
119124
120- ### 登陆的页面:
125+ ### 登陆的页面:
121126![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110154107624-1393804790.png )
122127
123- ### 登陆过后需要打开新的窗口,也就是打开百度指数,并且切换窗口,在selenium用:
128+ ### 登陆过后需要打开新的窗口,也就是打开百度指数,并且切换窗口,在selenium用:
124129```
125- # 新开一个窗口,通过执行js来新开一个窗口
130+ # 新开一个窗口,通过执行js来新开一个窗口
126131js = 'window.open("http://index.baidu.com");'
127132browser.execute_script(js)
128- # 新窗口句柄切换,进入百度指数
129- # 获得当前打开所有窗口的句柄handles
130- # handles为一个数组
133+ # 新窗口句柄切换,进入百度指数
134+ # 获得当前打开所有窗口的句柄handles
135+ # handles为一个数组
131136handles = browser.window_handles
132137# print(handles)
133- # 切换到当前最新打开的窗口
138+ # 切换到当前最新打开的窗口
134139browser.switch_to_window(handles[-1])
135140
136141```
137142
138- ### 清空输入框,构造点击天数:
143+ ### 清空输入框,构造点击天数:
139144```
140- # 清空输入框
145+ # 清空输入框
141146browser.find_element_by_id("schword").clear()
142- # 写入需要搜索的百度指数
147+ # 写入需要搜索的百度指数
143148browser.find_element_by_id("schword").send_keys(keyword)
144- # 点击搜索
149+ # 点击搜索
145150# <input type="submit" value="" id="searchWords" onclick="searchDemoWords()">
146151browser.find_element_by_id("searchWords").click()
147152time.sleep(2)
148- # 最大化窗口
153+ # 最大化窗口
149154browser.maximize_window()
150- # 构造天数
151- sel = int(input("查询7天请按0,30天请按1,90天请按2,半年请按3:"))
155+ # 构造天数
156+ sel = int(input("查询7天请按0,30天请按1,90天请按2,半年请按3:"))
152157day = 0
153158if sel == 0:
154159 day = 7
@@ -160,52 +165,52 @@ elif sel == 3:
160165 day = 180
161166sel = '//a[@rel="' + str(day) + '"]'
162167browser.find_element_by_xpath(sel).click()
163- # 太快了
168+ # 太快了
164169time.sleep(2)
165170```
166171
167- ### 天数也就是这里:
172+ ### 天数也就是这里:
168173![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110154603561-1092775179.png )
169174
170- ### 找到图形框:
175+ ### 找到图形框:
171176```
172177xoyelement = browser.find_elements_by_css_selector("#trend rect")[2]
173178```
174179
175- ### 图形框就是:
180+ ### 图形框就是:
176181![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110154817608-982142410.png )
177182
178- ### 根据坐标点的不同构造偏移量:
183+ ### 根据坐标点的不同构造偏移量:
179184![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110155142530-319352053.png )
180185
181- ### 选取7天的坐标来观察:
182- > 第一个点的横坐标为1031.66666
186+ ### 选取7天的坐标来观察:
187+ > 第一个点的横坐标为1031.66666
183188
184- > 第二个点的横坐标为1234
189+ > 第二个点的横坐标为1234
185190![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110155720764-1186100464.png )
186191
187- 所以7天两个坐标之间的差为:202.33,其他的天数类似
192+ 所以7天两个坐标之间的差为:202.33,其他的天数类似
188193
189- ### 用selenium库来模拟鼠标滑动悬浮:
194+ ### 用selenium库来模拟鼠标滑动悬浮:
190195```
191196from selenium.webdriver.common.action_chains import ActionChains
192197ActionChains(browser).move_to_element_with_offset(xoyelement,x_0,y_0).perform()
193198```
194199
195- ### 但是这样子确定的点指出是在这个位置:
200+ ### 但是这样子确定的点指出是在这个位置:
196201![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110155752202-120333333.png )
197202
198- 也就是矩形的左上角,这里是不会加载js显示弹出框的,所以要给横坐标+1:
203+ 也就是矩形的左上角,这里是不会加载js显示弹出框的,所以要给横坐标+1:
199204```
200205x_0 = 1
201206y_0 = 0
202207```
203208
204- ### 写个按照天数的循环,让横坐标累加:
209+ ### 写个按照天数的循环,让横坐标累加:
205210```
206- # 按照选择的天数循环
211+ # 按照选择的天数循环
207212for i in range(day):
208- # 构造规则
213+ # 构造规则
209214 if day == 7:
210215 x_0 = x_0 + 202.33
211216 elif day == 30:
@@ -216,73 +221,73 @@ for i in range(day):
216221 x_0 = x_0 + 6.78
217222```
218223
219- ### 鼠标横移时会弹出框,在网址里面找到这个框:
224+ ### 鼠标横移时会弹出框,在网址里面找到这个框:
220225![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110160257592-723215476.png )
221226
222- ### selenium自动识别之...:
227+ ### selenium自动识别之...:
223228```
224229# <div class="imgtxt" style="margin-left:-117px;"></div>
225230imgelement = browser.find_element_by_xpath('//div[@id="viewbox"]')
226231```
227232
228- ### 并且确定这个框的大小位置:
233+ ### 并且确定这个框的大小位置:
229234```
230- # 找到图片坐标
235+ # 找到图片坐标
231236locations = imgelement.location
232237print(locations)
233- # 找到图片大小
238+ # 找到图片大小
234239sizes = imgelement.size
235240print(sizes)
236- # 构造指数的位置
241+ # 构造指数的位置
237242rangle = (int(locations['x']), int(locations['y']), int(locations['x'] + sizes['width']),
238243 int(locations['y'] + sizes['height']))
239244```
240245
241246![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110160502389-924750650.png )
242247
243- ### 下面的思路就是:
244- > 1 . 将整个屏幕截图下来
248+ ### 下面的思路就是:
249+ > 1 . 将整个屏幕截图下来
245250
246- > 2 . 打开截图用上面得到的这个坐标rangle进行裁剪
251+ > 2 . 打开截图用上面得到的这个坐标rangle进行裁剪
247252
248- ### 但是最后裁剪出来的是上面的那个黑框,我想要的效果是:
253+ ### 但是最后裁剪出来的是上面的那个黑框,我想要的效果是:
249254![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110160724577-1831216031.jpg )
250255
251- ### 所以要对rangle进行计算,但是我懒,忽略了搜索词的长度,直接暴力的写成:
256+ ### 所以要对rangle进行计算,但是我懒,忽略了搜索词的长度,直接暴力的写成:
252257```
253- # 构造指数的位置
258+ # 构造指数的位置
254259rangle = (int(locations['x'] + sizes['width']/3), int(locations['y'] + sizes['height']/2), int(locations['x'] + sizes['width']*2/3),
255260 int(locations['y'] + sizes['height']))
256261```
257262
258- ### 这个写法最终不太好,最起码要对keyword的长度进行判断,长度过长会导致截图坐标出现偏差,反正我知道怎么做,就是不写出来给你们看!
263+ ### 这个写法最终不太好,最起码要对keyword的长度进行判断,长度过长会导致截图坐标出现偏差,反正我知道怎么做,就是不写出来给你们看!
259264
260- ### 后面的完整代码是:
265+ ### 后面的完整代码是:
261266```
262267# <div class="imgtxt" style="margin-left:-117px;"></div>
263268imgelement = browser.find_element_by_xpath('//div[@id="viewbox"]')
264- # 找到图片坐标
269+ # 找到图片坐标
265270locations = imgelement.location
266271print(locations)
267- # 找到图片大小
272+ # 找到图片大小
268273sizes = imgelement.size
269274print(sizes)
270- # 构造指数的位置
275+ # 构造指数的位置
271276rangle = (int(locations['x'] + sizes['width']/3), int(locations['y'] + sizes['height']/2), int(locations['x'] + sizes['width']*2/3),
272277 int(locations['y'] + sizes['height']))
273- # 截取当前浏览器
278+ # 截取当前浏览器
274279path = "../baidu/" + str(num)
275280browser.save_screenshot(str(path) + ".png")
276- # 打开截图切割
281+ # 打开截图切割
277282img = Image.open(str(path) + ".png")
278283jpg = img.crop(rangle)
279284jpg.save(str(path) + ".jpg")
280285```
281286
282- ### 但是后面发现裁剪的图片太小,识别精度太低,所以需要对图片进行扩大:
287+ ### 但是后面发现裁剪的图片太小,识别精度太低,所以需要对图片进行扩大:
283288```
284- # 将图片放大一倍
285- # 原图大小73.29
289+ # 将图片放大一倍
290+ # 原图大小73.29
286291jpgzoom = Image.open(str(path) + ".jpg")
287292(x, y) = jpgzoom.size
288293x_s = 146
@@ -291,23 +296,23 @@ out = jpgzoom.resize((x_s, y_s), Image.ANTIALIAS)
291296out.save(path + 'zoom.jpg', 'png', quality=95)
292297```
293298
294- 原图大小请 ** 右键->属性->详细信息** 查看,我的是长73像素,宽29像素
299+ 原图大小请 ** 右键->属性->详细信息** 查看,我的是长73像素,宽29像素
295300
296- ### 最后就是图像识别
301+ ### 最后就是图像识别
297302```
298- # 图像识别
303+ # 图像识别
299304index = []
300305image = Image.open(str(path) + "zoom.jpg")
301306code = pytesseract.image_to_string(image)
302307if code:
303308 index.append(code)
304309```
305310
306- ### 最后效果图:
311+ ### 最后效果图:
307312![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110161512889-300916957.png )
308313
309314![ ] ( http://images2015.cnblogs.com/blog/996148/201611/996148-20161110161525874-60911542.png )
310315
311316
312- ## 详细解说请观看我的博客:
313- [ TTyb] ( http://www.cnblogs.com/TTyb )
317+ ## 详细解说请观看我的博客:
318+ [ TTyb] ( http://www.cnblogs.com/TTyb )
0 commit comments