import requests from lxml import etree import mysql_Helper myhelper = mysql_Helper.MysqlHelper() sql = 'INSERT INTO tencent (title, zhineng, renshu, didian,fabu_time) VALUES' \ ' (%s, %s, %s, %s,%s)' # url = 'https://hr.tencent.com/position.php?keywords=&lid=2156&start=0#a' headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9", "Cache-Control": "no-cache", "Connection": "keep-alive", "Cookie": "pgv_pvi=5854498816; _ga=GA1.2.608623393.1534496276; pt2gguin=o1900227304; PHPSESSID=0smi013v1lr7r3ki2aqtacp493; pgv_si=s8414673920", "Host": "hr.tencent.com", "Pragma": "no-cache", "Referer": "https://hr.tencent.com/position.php?&start=10", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36", } # url = 'https://hr.tencent.com/position.php?keywords=&lid=2156&start=0#a' base_url = 'https://hr.tencent.com/position.php?keywords=&tid=0&lid=2156&start=%s#a' # response = requests.get(base_url,headers=headers) # with open('tengxun.html','wb') as f: # f.write(response.content) for i in range(0,300,10): url = base_url % i response = requests.get(url, headers=headers) html_ele = response.text # print(html_ele) ver = etree.HTML(html_ele) # print(ver) for v in range(2,12): li_list = ver.xpath('//div[@id="position"]/div/table/tr[{}]'.format(v)) # print(li_list) for li_ele in li_list: # if li_ele == li_ele.xpath('./tr[1]')[0].text: # continue title = li_ele.xpath('./td/a')[0].text print(title) zhineng = li_ele.xpath('./td[2]')[0].text print(zhineng) renshu = li_ele.xpath('./td[3]')[0].text print(renshu) didian = li_ele.xpath('./td[4]')[0].text print(didian) fabu_time = li_ele.xpath('./td[5]')[0].text print(fabu_time) data = (title, zhineng, renshu, didian,fabu_time) myhelper.execute_modify_sql(sql, data)
import pymysql class MysqlHelper(object): def __init__(self): self.db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='lxh1122', database='py11', charset='utf8') # self.db = pymysql.connect(host='127.0.0.1',port='3306',user='root',password='lxh1122',database='py11',charset='utf8') self.cursor = self.db.cursor() def execute_modify_sql(self,sql,data): self.cursor.execute(sql,data) self.db.commit() def __del__(self): self.cursor.close() self.db.close() if __name__=='__main__': conn = MysqlHelper() conn.execute_modify_sql('insert into wawj(title) VALUE (%s)', data=('aabbccdd'))