python3 使用硒模拟登录天眼查抓取数据
最编程
2024-06-29 22:29:10
...
由于之前用Scrapy 抓了一些公司的名称,但是没有准确的联系方式,所以就自己就学习了一下使用selenium自动化工具,速度比较慢,网上也有很多这方面的代码,但是大部分的网页解析部分都出错了,可能是这种网站定时会更改一下网页的固定几个标签。
网上也有很多说如果遇到一些防爬虫特别强的网站,比如企查查...使用了滑动验证和图像验证码选择,实在是逆天,对于刚接触爬虫还是不要去选择攻破,可以考虑通过selenium打开窗口,用time模块延时,然后手动登录,然后获取当前登录的cookie,之后再通过其他模块或者框架,利用cookie免登录进去爬取。这个思路倒是很不错,过段时间去学习一下。
这个小项目还是有很多缺点的,比如访问时间太快,访问的时间统一。如果连续点击大概40多个公司后就会检测到,怀疑是机器人,然后就要进行图像验证。不过应该可以用random个随机时间用time模块,这样子来模拟更真实的人操作。
# -*- coding: UTF-8 -*- 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 threading #————————————天眼查大类———————————————— class Tianyan(): def __init__(self,user,pwd): self.browser=webdriver.Firefox() #实例化浏览器对象,并命名为 browser self.user=user self.pwd=pwd self.wait=WebDriverWait(self.browser,5) #设定浏览器最大等待时间为5秒钟,超过就报错 self.get_url() #————————————触发浏览器对象———————————— def get_url(self): self.browser.get("https://www.tianyancha.com/") #打开天眼查浏览器 button=self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME,"link-white"))) #等待目标可以点击 button.click() button2=self.wait.until(EC.element_to_be_clickable((By.XPATH,'//div[@class="login-warp"]/div[1]/div[3]/div[1]/div[2]'))) button2.click() #———————————公司文本———————————— def write(self): with open(doc_company,"rb") as f: for i in f : yield i.decode(encoding='utf-8') #———————————登录—————————————————— def check_login(self): try: input_user = self.browser.find_element_by_xpath('//div[@class="login-warp"]/div[1]/div[3]/div[2]/div[2]/input') input_psw = self.browser.find_element_by_xpath('//div[@class="login-warp"]/div[1]/div[3]/div[2]/div[3]/input') input_user.send_keys(self.user) #发送登录账号 input_psw.send_keys(self.pwd) time.sleep(1) #等待 一秒 方式被识别为机器人 login=self.wait.until(EC.element_to_be_clickable((By.XPATH,'//div[@class="login-warp"]/div[1]/div[3]/div[2]/div[5]'))) login.click() return True except Exception: return False # ———————————发送要查询公司的名称—————————————————— def check_company(self,company): company_input = self.browser.find_element_by_id("home-main-search") company_click = self.wait.until(EC.element_to_be_clickable((By.XPATH,'//div[@class="input-group-btn btn -hg"]'))) company_input.send_keys(company) time.sleep(0.2) company_click.click() # ————————————获取要查询的公司的名称、法人、电话信息————————————————— def get_news(self): name = self.browser.find_element_by_xpath('//div[@class="header"]/a/em').text faren = self.browser.find_element_by_xpath('//div[@class="info"]/div[1]/a').text phone = self.browser.find_element_by_xpath('//div[@class="contact"]/div[1]/span[2]').text company_list = "公司名称:|%s |法人:| %s|电话:| %s" % (name, faren, phone) self.downlode_company(company_list) #存储信息 print(company_list) #———————————存储需要时保存的信息—————————————————— def downlode_company(self,data): with open(doc_check_company, "a", encoding="utf-8") as f: f.write(data) f.write("\n") # ———————————主要运行逻辑—————————————————— def main(self): if self.check_login(): # f=self.write() for company in f: try: self.check_company(company) get_thread=threading.Thread(target=self.get_news()) #使用线程来存储信息 get_thread.start() self.browser.back() except: message = '公司名称:|%s|该公司电话法人资料不齐全,无法抓取' % company print(message) self.downlode_company(message) self.browser.back() else: print("账号密码不正确,请重新核对") #———————————主体参数——————————————————————— if __name__ == '__main__': user= #账号 pwd='' #密码 doc_check_company='check_company.txt' #查询之后的公司名称 doc_company='company.txt' #需要查询的公司列表 time1=time.time() window=Tianyan(user,pwd) window.main() time2=time.time() print(time2-time1)
刚学了下生成器的概念,在这里就用了一下yield,还用了一下线程。