通过win32api与win32con模拟按键精灵爬取中国商标网数据

# -*- coding: utf-8 -*-
# Author: Yakuho
# Date  : 2019/8/8
import asyncio
import os
import time
import random
from selenium import webdriver
import button
import win32api
import win32con
import queue
# id_q = (x for x in range(int(input('想要爬取的起始注册号:')), int(input('想要爬取的最后注册号:')) + 1))
info_q = queue.Queue()


class Detail:
    def __init__(self):
        self.url = 'http://sbj.cnipa.gov.cn/'
        self.executable_path = os.path.abspath('chromedriver.exe')
        self.options = webdriver.ChromeOptions()
        self.options.add_argument('--disable-gpu')
        self.options.add_experimental_option('excludeSwitches', ['enable-automation'])
        # self.options.add_argument('proxy-server=' + proxy)
        self.browser = webdriver.Chrome(executable_path=self.executable_path, options=self.options)
        self.position = (0, 0)

    async def brand_check(self):
        self.browser.set_script_timeout(4)
        self.browser.set_page_load_timeout(4)
        try:
            self.browser.get(self.url)
        except:
            pass
        win32api.SetCursorPos((0, 0))
        # 商标查询
        for i in button.button1:
            win32api.SetCursorPos(i)
            time.sleep(random.randint(2, 4) * 0.001)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
        self.browser.close()  # 关闭第一个页面
        await asyncio.sleep(4)
        # 我同意
        for i in button.button2:
            win32api.SetCursorPos(i)
            time.sleep(random.randint(2, 4) * 0.001)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
        await asyncio.sleep(4)
        # 综合查询
        for i in button.button3:
            win32api.SetCursorPos(i)
            time.sleep(random.randint(2, 4) * 0.001)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
        await asyncio.sleep(6)

    async def main(self):
        await self.brand_check()
        infos = ['135001', '1350001']
        for info in infos:
            # info = id_q.get()
            # 点击输入框
            win32api.SetCursorPos((426, 415))
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
            # 清空文本框
            win32api.keybd_event(17, 0, 0, 0)
            win32api.keybd_event(65, 0, 0, 0)
            win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
            win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)
            win32api.keybd_event(8, 0, 0, 0)
            win32api.keybd_event(8, 0, win32con.KEYEVENTF_KEYUP, 0)
            # 输入识别号
            for i in info:
                win32api.keybd_event(ord(i), 0, 0, 0)
                win32api.keybd_event(ord(i), 0, win32con.KEYEVENTF_KEYUP, 0)
            # 点击查询
            for i in button.button4_click:
                win32api.SetCursorPos(i)
                time.sleep(random.randint(2, 4) * 0.001)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
            # 点击查询的项目
            win32api.SetCursorPos((126, 202))  # 输入框位置
            await asyncio.sleep(4)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
            win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
            if self.browser.window_handles[-2] == 2:
                self.browser.switch_to.window(self.browser.window_handles[-1])
                self.browser.close()
            else:
                await asyncio.sleep(4)
                self.browser.switch_to.window(self.browser.window_handles[-2])
                print(self.browser.page_source)
                # info_q.put(self.browser.page_source)
                self.browser.close()
                self.browser.switch_to.window(self.browser.window_handles[-1])
                self.browser.close()


if __name__ == "__main__":
    progress1 = Detail()
    # progress2 = Detail()
    tasks = [progress1.main()]
    asyncio.get_event_loop().run_until_complete(asyncio.gather(*tasks))
# -*- coding: utf-8 -*-
# Author: Yakuho
# Date  : 2019/8/9
button1 = [(0, 1),  (0, 2),  (0, 3),  (0, 5),  (0, 9),  (1, 12),  (1, 16),  (2, 18),  (2, 20),  (2, 21),  (2, 24),
           (2, 25),  (2, 26),  (2, 29),  (2, 32),  (3, 35),  (3, 39),  (3, 41),  (5, 45),  (6, 51),  (7, 55),
           (8, 58),  (9, 62),  (11, 70),  (11, 71),  (14, 78),  (16, 82),  (22, 89),  (23, 96),  (27, 101),
           (28, 106),  (30, 110),  (31, 113),  (32, 118),  (34, 124),  (37, 130),  (40, 137),  (43, 144),
           (47, 153),  (50, 160),  (53, 168),  (57, 175),  (62, 183),  (66, 189),  (69, 196),  (71, 200),
           (75, 206),  (78, 212),  (82, 218),  (86, 227),  (86, 229),  (87, 233),  (89, 240),  (91, 247),
           (96, 253),  (101, 261),  (105, 268),  (108, 274),  (112, 282),  (117, 289),  (124, 297),  (129, 306),
           (133, 315),  (139, 324),  (144, 333),  (149, 342),  (155, 353),  (160, 362),  (164, 371),  (168, 381),
           (170, 388),  (173, 397),  (175, 403),  (177, 410),  (181, 419),  (185, 428),  (187, 435),  (191, 442),
           (193, 450),  (197, 459),  (202, 468),  (206, 477),  (209, 485),  (211, 496),  (213, 504),  (215, 512),
           (218, 520),  (220, 529),  (224, 538),  (228, 547),  (230, 554),  (233, 563),  (235, 570),  (237, 577),
           (239, 583),  (240, 588),  (241, 595),  (243, 604),  (244, 611),  (244, 618),  (244, 626),  (244, 631),
           (247, 639),  (248, 640),  (249, 643),  (250, 646),  (252, 654),  (252, 655),  (256, 667),  (256, 671),
           (256, 677),  (256, 687),  (256, 690),  (256, 695),  (256, 699),  (259, 703),  (263, 708),  (264, 713),
           (267, 717),  (268, 723),  (271, 727),  (272, 734)]
button2 = [(270, 743), (271, 743), (271, 743), (272, 743), (273, 743), (274, 743), (275, 743), (278, 742), (281, 742),
           (281, 742), (284, 741), (290, 740), (293, 739), (294, 739), (294, 739), (298, 738), (302, 736), (306, 735),
           (308, 734), (310, 734), (310, 734), (313, 734), (316, 734), (318, 734), (318, 734), (321, 734), (324, 734),
           (326, 734), (329, 733), (332, 732), (337, 731), (342, 730), (342, 730), (346, 730), (350, 728), (355, 727),
           (355, 727), (355, 727), (359, 727), (362, 726), (366, 726), (371, 725), (375, 725), (375, 725), (379, 725),
           (382, 724), (386, 723), (390, 723), (395, 722), (395, 722), (398, 722), (401, 722), (402, 721), (406, 720),
           (409, 720), (411, 720), (411, 720), (413, 719), (416, 718), (419, 718), (424, 718), (424, 718), (427, 718),
           (430, 717), (434, 716), (437, 715), (437, 715), (440, 714), (441, 714), (442, 714), (445, 714), (445, 714),
           (449, 713), (450, 713), (453, 713), (457, 712), (459, 712), (459, 712), (462, 712), (463, 711), (465, 711),
           (467, 710), (470, 709), (471, 708), (471, 708), (474, 708), (477, 707), (481, 706), (482, 706), (482, 706),
           (482, 706), (483, 706), (485, 705), (486, 704), (486, 704), (488, 702), (489, 702), (490, 702), (491, 702),
           (494, 700), (494, 699), (495, 699), (495, 699), (498, 698), (498, 698), (498, 698), (498, 697), (498, 697),
           (499, 696), (499, 696), (502, 694), (502, 694), (502, 694), (504, 694), (506, 693), (506, 693), (509, 692),
           (509, 690), (509, 690), (509, 690), (509, 690), (509, 690), (509, 690), (509, 690), (509, 690), (509, 690),
           (509, 690), (510, 690), (510, 690), (511, 690), (511, 690), (512, 688), (512, 688), (513, 687), (513, 687),
           (513, 687), (513, 687), (513, 687), (513, 687), (513, 687), (513, 687), (513, 687)]
button3 = [(512, 682), (514, 678), (514, 676), (515, 674), (517, 670), (518, 668), (518, 668), (519, 668), (519, 667),
           (520, 663), (521, 662), (522, 660), (523, 658), (523, 658), (523, 655), (525, 654), (526, 651), (526, 647),
           (527, 642), (527, 642), (528, 641), (528, 635), (529, 634), (531, 629), (531, 625), (532, 622), (532, 622),
           (533, 618), (534, 615), (534, 611), (534, 610), (535, 606), (535, 606), (536, 602), (536, 598), (537, 595),
           (538, 591), (538, 590), (538, 590), (538, 587), (538, 582), (538, 578), (538, 577), (538, 573), (540, 569),
           (541, 567), (541, 567), (541, 564), (541, 562), (541, 561), (542, 555), (542, 554), (542, 554), (542, 551),
           (542, 549), (542, 546), (542, 546), (542, 543), (543, 541), (543, 541), (543, 538), (545, 536), (545, 534),
           (545, 531), (546, 530), (546, 529), (546, 526), (546, 526), (546, 523), (546, 521), (546, 518), (547, 517),
           (548, 514), (548, 514), (548, 512), (548, 511), (548, 509), (548, 506), (548, 503), (548, 503), (548, 499),
           (549, 498), (549, 496), (549, 495), (550, 492), (550, 490), (550, 490), (550, 486), (550, 486), (550, 485),
           (550, 484), (550, 481), (550, 479), (550, 479), (550, 478), (550, 476), (551, 474), (551, 474), (551, 474),
           (551, 474), (551, 473), (551, 471), (552, 470), (552, 470), (552, 467), (552, 467), (552, 466), (552, 466),
           (552, 466), (553, 465), (553, 463), (553, 462), (554, 461)]
button4_click = [(426, 415), (426, 420), (426, 420), (428, 425), (429, 427), (431, 431), (434, 433), (435, 437), (437, 440),
           (437, 440), (437, 440), (439, 446), (439, 451), (442, 455), (442, 460), (442, 460), (444, 462), (445, 465),
           (446, 467), (446, 470), (446, 470), (447, 473), (448, 476), (449, 481), (450, 484), (450, 485), (450, 485),
           (450, 486), (452, 490), (453, 492), (453, 493), (454, 494), (454, 494), (455, 495), (456, 497), (457, 500),
           (458, 500), (458, 500), (458, 502), (458, 504), (458, 505), (459, 506), (462, 510), (462, 510), (462, 512),
           (463, 515), (464, 518), (465, 521), (465, 521), (466, 522), (467, 524), (468, 527), (472, 530), (473, 531),
           (473, 531), (474, 534), (474, 535), (475, 538), (477, 540), (477, 541), (481, 546), (481, 546), (481, 549),
           (482, 552), (483, 554), (484, 555), (484, 555), (484, 558), (485, 559), (485, 560), (485, 562), (486, 564),
           (486, 564), (486, 566), (487, 568), (487, 569), (487, 571), (487, 571), (487, 573), (488, 575), (489, 579),
           (490, 585), (490, 585), (490, 586), (490, 592), (491, 595), (491, 598), (491, 600), (491, 600), (491, 602),
           (492, 604), (492, 608), (492, 610), (492, 610), (492, 613), (492, 616), (492, 619), (492, 621), (492, 623),
           (492, 623), (492, 626), (492, 628), (494, 630), (494, 633), (494, 634), (494, 634), (494, 634), (495, 637),
           (496, 638), (498, 640), (498, 640), (498, 641), (499, 642), (499, 642), (499, 642), (500, 644), (500, 644),
           (500, 644), (501, 645), (501, 645)]
button4_check = [(501, 645), (501, 645), (500, 644), (500, 644), (500, 644), (499, 642), (499, 642), (499, 642),
                 (498, 641), (498, 640), (498, 640), (496, 638), (495, 637), (494, 634), (494, 634), (494, 634),
                 (494, 633), (494, 630), (492, 628), (492, 626), (492, 623), (492, 623), (492, 621), (492, 619),
                 (492, 616), (492, 613), (492, 610), (492, 610), (492, 608), (492, 604), (491, 602), (491, 600),
                 (491, 600), (491, 598), (491, 595), (490, 592), (490, 586), (490, 585), (490, 585), (489, 579),
                 (488, 575), (487, 573), (487, 571), (487, 571), (487, 569), (487, 568), (486, 566), (486, 564),
                 (486, 564), (485, 562), (485, 560), (485, 559), (484, 558), (484, 555), (484, 555), (483, 554),
                 (482, 552), (481, 549), (481, 546), (481, 546), (477, 541), (477, 540), (475, 538), (474, 535),
                 (474, 534), (473, 531), (473, 531), (472, 530), (468, 527), (467, 524), (466, 522), (465, 521),
                 (465, 521), (464, 518), (463, 515), (462, 512), (462, 510), (462, 510), (459, 506), (458, 505),
                 (458, 504), (458, 502), (458, 500), (458, 500), (457, 500), (456, 497), (455, 495), (454, 494),
                 (454, 494), (453, 493), (453, 492), (452, 490), (450, 486), (450, 485), (450, 485), (450, 484),
                 (449, 481), (448, 476), (447, 473), (446, 470), (446, 470), (446, 467), (445, 465), (444, 462),
                 (442, 460), (442, 460), (442, 455), (439, 451), (439, 446), (437, 440), (437, 440), (437, 440),
                 (435, 437), (434, 433), (431, 431), (429, 427), (428, 425), (426, 420), (426, 420), (426, 415)]
button5 = [(329, 454),
(330, 455),
(332, 456),
(334, 458),
(335, 458),
(337, 459),
(338, 461),
(340, 463),
(342, 465),
(342, 467),
(344, 467),
(344, 469),
(345, 469),
(345, 469),
(345, 469),
(345, 470),
(345, 470),
(347, 472),
(348, 474),
(350, 476),
(351, 478),
(351, 478),
(352, 479),
(353, 480),
(353, 481),
(355, 481),
(355, 483),
(357, 483),
(359, 483),
(361, 484),
(363, 486),
(365, 487),
(365, 488),
(366, 490),
(367, 490),
(369, 490),
(369, 490),
(369, 490),
(371, 490),
(373, 491),
(375, 493),
(376, 494),
(379, 494),
(380, 494),
(381, 496),
(381, 498),
(381, 498),
(381, 498),
(381, 499),
(383, 499),
(384, 501),
(385, 501),
(385, 502),
(388, 504),
(388, 505),
(389, 505),
(390, 506),
(390, 507),
(390, 507),
(392, 507),
(392, 507),
(392, 508),
(393, 510),
(393, 512),
(393, 514),
(393, 516),
(395, 518),
(395, 519),
(396, 521),
(396, 523),
(396, 525),
(397, 526),
(397, 526),
(399, 528),
(401, 530),
(401, 532),
(402, 534),
(402, 536),
(404, 536),
(404, 538),
(405, 538),
(406, 539),
(407, 541),
(409, 541),
(409, 543),
(411, 543),
(412, 545),
(413, 547),
(414, 548),
(414, 548),
(415, 550),
(417, 550),
(418, 551),
(418, 553),
(418, 554),
(419, 556),
(421, 556),
(422, 556),
(423, 558),
(425, 560),
(426, 562),
(428, 564),
(430, 564),
(432, 565),
(433, 566),
(434, 566),
(435, 567),
(437, 567),
(439, 567),
(441, 569),
(443, 570),
(443, 572),
(445, 573),
(445, 575),
(445, 577),
(445, 579),
(447, 581),
(447, 583),
(449, 583),
(449, 585),
(452, 585),
(453, 587),
(453, 589),
(453, 590),
(453, 592),
(456, 593),
(457, 595),
(458, 596),
(459, 596),
(461, 597),
(462, 597),
(464, 598),
(464, 600),
(464, 600),
(464, 601),
(466, 603),
(468, 604),
(470, 604),
(471, 606),
(471, 607),
(471, 609),
(473, 611),
(475, 613),
(477, 613),
(477, 614),
(479, 614),
(481, 614),
(483, 615),
(484, 615),
(484, 617),
(485, 618),
(486, 618),
(486, 618),
(487, 619),
(487, 621),
(488, 621),
(488, 622),
(488, 622),
(489, 624),
(489, 624),
(490, 625),
(490, 627),
(490, 628),
(492, 630),
(493, 630),
(494, 632),
(494, 634),
(494, 634),
(495, 634),
(495, 635),
(497, 637),
(497, 639),
(499, 640),
(500, 641),
(500, 642),
(502, 642),
(504, 644),
(506, 644),
(508, 644),
(508, 644),
(508, 644),
(510, 645),
(511, 647),]

 

posted @ 2019-08-20 09:13  达尔文在思考  阅读(1069)  评论(0编辑  收藏  举报