import pymysql
class MysqlHelper(object):
def __init__(self):
self.db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123456', database='py1011', 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()
import requests
import re
from lxml import etree
import mysqlhelper
myhelper = mysqlhelper.MysqlHelper()
sql = 'INSERT INTO huilongguan (title, buju, location, status) VALUES (%s, %s, %s, %s)'
for i in range(1,4):
url = 'https://bj.5i5j.com/zufang/huilongguan/n%s/' %(i)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36',
'Cookie': 'PHPSESSID=4qsda5a2lv8k3ls8rc46impdsc; domain=bj; _ga=GA1.2.1446330960.1534510715; _gid=GA1.2.893383312.1534510715; yfx_c_g_u_id_10000001=_ck18081720583916687035717136772; Hm_lvt_94ed3d23572054a86ed341d64b267ec6=1534510728; _Jo0OQK=4786B52FDDFD00A8A3F8FBA45890F5281E90029D464855392F7B77060188839F312A26F03675483B8589CBAB4ED2B2F3F4B96D48A9FE444B19028F1FA8B868E4153C57212F12283777C840763663251ADEB840763663251ADEBB45458312D3818AD2DBEB50F8D291FCBGJ1Z1IQ==; zufang_BROWSES=41291662; yfx_f_l_v_t_10000001=f_t_1534510719651__r_t_1534510719651__v_t_1534512823806__r_c_0; Hm_lpvt_94ed3d23572054a86ed341d64b267ec6=1534513475'
}
response = requests.get(url,headers=headers)
html = response.text
html_ele = etree.HTML(response.text)
li_list = html_ele.xpath('//ul[@class="pList"]/li')
for li_ele in li_list:
title = li_ele.xpath('./div[2]/h3/a')[0].text
print(title)
buju = li_ele.xpath('./div[2]/div[1]/p[1]/text()')[0]
print(buju)
try:
location = li_ele.xpath('./div[2]/div[1]/p[2]/text()')[0]
except:
location = '你SB吗?连地址都不写!?'
print(location)
status = li_ele.xpath('./div[2]/div[1]/p[3]/text()')[0]
print(status)
print('-'*50)
data = (title, buju, location, status)
myhelper.execute_modify_sql(sql, data)