恋家-bs4

from bs4 import BeautifulSoup
import requests
from day3.mysql_test import mysql_conn

mc = mysql_conn()

url = 'https://zh.lianjia.com/zufang/rp5/'

response = requests.get(url)

# with open('lianjia.html', 'wb') as f:
#     f.write(response.content)

#通过html的字符串,获取BeautifulSoup对象
soup = BeautifulSoup(response.text, 'lxml')

div_list = soup.find('div', class_ = "list-wrap")

ul_house = div_list.find('ul', class_= "house-lst")

gezhong_info = ul_house.find_all('div', class_='info-panel')
# print(gezhong_info)
for div_tag in gezhong_info:
    # print(div_tag)
    a_title = div_tag.select('h2 > a')[0]
    a=a_title['title']
    b_region = div_tag.select('div.col-1 > div.where')[0].text
    # print(b_region)
    # print(type(b_region))
    c_other = div_tag.select('div.other > div.con')[0].text
    # print(c_other)
    sql = 'insert into zufang(a_title, b_region, c_other) value (%s,%s,%s)'

    data = [a,b_region,c_other]
    mc.execute_modify_mysql(sql,data)

  

posted on 2018-08-23 09:05  Pyton当歌人生几何  阅读(96)  评论(0编辑  收藏  举报

导航