import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
import time
import random
import json
import re
import pymysql
from tqdm import tqdm
class mySpider:
def __init__(self, url):
self.url = url
self.count = 1
self.db = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='logicfeng',
database='test2')
self.cursor = self.db.cursor()
def __del__(self):
self.cursor.close()
self.db.close()
print("关闭数据库!")
def getHeader(self):
ua = UserAgent()
headers = {
'User-Agent': ua.random}
return headers
def getBody(self, url, send_type, data):
if self.count <= 3:
try:
if send_type == 'get':
res = requests.get(url=url, headers=self.getHeader(), params=data, timeout=2)
elif send_type == 'post':
res = requests.post(url=url, headers=self.getHeader(), data=data, timeout=2)
else:
print("未输入send_type,直接返回None")
res = None
return res
except Exception as e:
print(e)
self.count += 1
print(f"第{self.count}次,发起请求")
return self.getBody(url, send_type, data)
def parseData(self, dataList):
for row in tqdm(dataList, desc='爬取进度'):
urlDetail = f"https://www.baidu.com/CTMDS/pub/PUB010100.do?method=handle04&compId={row['companyId']}"
self.count = 1
res = self.getBody(url=urlDetail, send_type='get', data={
})
if res is not None:
self.parseHtml(row=row, htmlText=res.text)
else:
print(f"{urlDetail}请求失败!")
def parseHtml(self, row, htmlText):
soup = BeautifulSoup(htmlText, 'html.parser')
divList = soup.find_all('div', class_=['col-md-8'])
divtextList = [re.sub(r'\s+', '', div.text) for div in divList]
divListOther = soup.find_all('div', class_=['col-sm-8'])
divtextListOther = [re.sub(r'\s+', '', div.text) for div in divListOther]
otherOrgAdd = ','.join(divtextListOther)
companyId = row['companyId']
linkTel = row['linkTel']
recordNo = row['recordNo']
areaName = row['areaName']
linkMan = row['linkMan']
address = row['address']
compName = row['compName']
recordStatus = row['recordStatus']
cancelRecordTime = row.get('cancelRecordTime', '')
compLevel = divtextList[2]
recordTime = divtextList[6]
sql1 = "insert INTO medical_register(company_id,area_name,record_no,comp_name,address,link_man,link_tel,record_status,comp_level,record_time,cancel_record_time,other_org_add) "
sql2 = f"values('{companyId}','{areaName}','{recordNo}','{compName}','{address}','{linkMan}','{linkTel}','{recordStatus}','{compLevel}','{recordTime}','{cancelRecordTime}','{otherOrgAdd}')"
sql3 = sql1 + sql2
self.cursor.execute(sql3)
self.db.commit()
tbody = soup.find('tbody')
trList = tbody.find_all('tr')
for tr in trList:
tdList = tr.find_all('td')
tdTextList = [td.text for td in tdList]
tdTextList.insert(0, companyId)
sql4 = "insert into medical_register_sub (company_id,professional_name,principal_investigator,job_title) values(%s,%s,%s,%s)"
self.cursor.execute(sql4, tdTextList)
self.db.commit()
def run(self):
try:
data = {
'pageSize': 1350, 'curPage': 1}
self.count = 1
res = self.getBody(url=self.url, send_type='post', data=data)
if res is not None:
jsonRes = json.loads(res.text)
status = jsonRes['success']
if status == True:
dataList = jsonRes['data']
self.parseData(dataList=dataList)
else:
print(f"{self.url}请求失败")
except Exception as e:
print('发生错误!', e)
if __name__ == '__main__':
spider = mySpider('https://www.百度.com/CTMDS/pub/PUB010100.do?method=handle05')
spider.run()