我爱我家

Posted lxh777

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我爱我家相关的知识,希望对你有一定的参考价值。

import requests
from lxml import etree
import mysql_Helper
myhelper = mysql_Helper.MysqlHelper()
sql = INSERT INTO wawj (title, daxiao, weizhi, money) VALUES        (%s, %s, %s, %s)

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": "_ga=GA1.2.688317442.1534488686; _gid=GA1.2.972294738.1534488686; yfx_c_g_u_id_10000001=_ck18081714512712257910311303078; yfx_mr_f_n_10000001=baidu%3A%3Amarket_type_cpc%3A%3A%3A%3A%3A%3A%3A%3A%3A%3A%25E5%258C%2597%25E4%25BA%25AC%25E7%25A7%259F%25E6%2588%25BF%3A%3Abj.5i5j.com%3A%3A17492183659%3A%3A%3A%3A%25E7%25A7%259F%25E6%2588%25BF%25E7%25B2%25BE%25E7%25A1%25AE%3A%3A%25E5%258C%2597%25E4%25BA%25AC%25E7%25A7%259F%25E6%2588%25BF%3A%3A36%3A%3Apmf_from_adv%3A%3Abj.5i5j.com%2F; _Jo0OQK=5E4EE870380D8ED7FADF80D748A2FDDCDEFE7DDDD60E6D937069F17096411098BE763C99E619117284762FF2394480C92859B339BBCEE71196FB6C85CFF0D6520ACC57212F12283777C840763663251ADEB840763663251ADEB0F3AF81242A0309E5FAFDAF9B9801008GJ1Z1dw==; zufang_BROWSES=41249527%2C40819878%2C90118784%2C90074025%2C90070238%2C90117560%2C90117454%2C41286051%2C41278407; phpSESSID=8gbj4p5us1fgk0p5tfpfobgla3; domain=bj; yfx_f_l_v_t_10000001=f_t_1534488687212__r_t_1534488687212__v_t_1534504639146__r_c_0; yfx_mr_n_10000001=baidu%3A%3Amarket_type_ppzq%3A%3A%3A%3Abaidu_ppc%3A%3A%25e6%2588%2591%25e7%2588%25b1%25e6%2588%2591%25e5%25ae%25b6%3A%3A%3A%3A%25E6%25A0%2587%25E9%25A2%2598%3A%3Awww.baidu.com%3A%3A%3A%3A%3A%3A%25E5%25B7%25A6%25E4%25BE%25A7%25E6%25A0%2587%25E9%25A2%2598%3A%3A%25E6%25A0%2587%25E9%25A2%2598%3A%3A160%3A%3Apmf_from_adv%3A%3Abj.5i5j.com%2F; yfx_key_10000001=%25e6%2588%2591%25e7%2588%25b1%25e6%2588%2591%25e5%25ae%25b6; Hm_lvt_94ed3d23572054a86ed341d64b267ec6=1534488689,1534489480,1534504645; Hm_lpvt_94ed3d23572054a86ed341d64b267ec6=1534504868",
    # "Host": "bj.5i5j.com",
    # "Pragma": "no-cache",
    # "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",
}

base_url = https://bj.5i5j.com/zufang/changpingqu/p7n%s/


# with open(‘wiwj.html‘,‘wb‘) as f:
#     f.write(response.content)

for i in range(1,4):
    url = base_url % i
    response = requests.get(url, headers=headers)
    html_ele = response.text

    ver = etree.HTML(html_ele)
    # print(ver)
    li_list = ver.xpath(//ul[@class="pList"]/li)

    for html_ele in li_list:
        title = html_ele.xpath(./div[2]/h3/a)[0].text
        print(title)
        daxiao = html_ele.xpath(./div[2]/div/p/text())[0]
        print(daxiao)
        #判断是否有第三条数据
        try:
            weizhi = html_ele.xpath(./div[2]/div[1]/p[2]/a/text())[0] + html_ele.xpath(./div[2]/div[1]/p[2]/text())[0]
            print(weizhi)
        except:
            weizhi = html_ele.xpath(./div[2]/div[1]/p[2]/a/text())[0]
            print(weizhi)

        money = html_ele.xpath(./div[2]/div/div/p/strong)[0].text
        print(money)
        data = (title, daxiao, weizhi, money)
        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))

 

以上是关于我爱我家的主要内容,如果未能解决你的问题,请参考以下文章

代码笔记获取字符串的宽度,高度

我爱写代码 | 每日趣闻

一生一世表白的话用python写?

我爱打代码,打代码使我快乐

我爱java系列---自定义注解

我爱java系列---idea中如何使用git