import requestsfrom lxml import etreefrom day3.mysql_test import mysql_connmyhelper = mysql_conn()sql = 'insert into woaiwojia(target,title,title_1,title_3,strong) values (%s, %s, %s ,%s, %s)'headers = { 'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'}for i in range(1,4): url ='https://bj.5i5j.com/zufang/huilongguan/n{}/'.format(i) # print(url) response = requests.get(url, headers=headers) # with open('woaiwojia.html', 'wb') as f: # f.write(response.content) html_ele = etree.HTML(response.text) # print(html_ele) li_list = html_ele.xpath('//ul[@class="pList"]/li') # print(li_list) for li_ele in li_list: # print(li_ele) target = li_ele.xpath('./div[2]/h3/a')[0].text # print(target) title = li_ele.xpath('./div[2]/div[1]/p[1]/text()')[0] # print(title) title_1 = li_ele.xpath('./div[2]/div[1]/p[2]/a/text()')[0] + li_ele.xpath('./div[2]/div[1]/p[2]/text()')[0] # print(title_1) title_2 = li_ele.xpath('./div[2]/div[2]/span/text()') for v in title_2: title_3 = v # print(v) strong = li_ele.xpath('./div[2]/div[1]/div[1]/p/strong')[0].text # print(strong) data = (target,title,title_1,title_3,strong) myhelper.execute_modify_mysql(sql, data)