Python实战计划学习作业1-3

代码如下:

from bs4 import BeautifulSoup
import requests

urls = ["http://bj.xiaozhu.com/search-duanzufang-p{}-0/".format(str(i)) for i in range(2, 5, 1)]
urls.insert(0, "http://bj.xiaozhu.com/")
max_of_pages = 30


def get_detail_page_url(url):
    wb_data = requests.get(url)
    soup = BeautifulSoup(wb_data.text, 'lxml')
    page_urls = soup.select("#page_list > ul > li > a")
    return [detail_page_url.get("href") for detail_page_url in page_urls]


def analyze_detail_page(url_str, num):
    page_data = requests.get(url_str)
    soup = BeautifulSoup(page_data.text, 'lxml')

    title = soup.select("body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > h4 > em")
    address = soup.select("body > div.wrap.clearfix.con_bg > div.con_l > div.pho_info > p")
    monthly_rental = soup.select("#pricePart > div.day_l > span")
    room_image = soup.select("#curBigImage")
    owner_image = soup.select("#floatRightBox > div.js_box.clearfix > div.member_pic > a > img")
    owner_name = soup.select("#floatRightBox > div.js_box.clearfix > div.w_240 > h6 > a")
    owner_sex_blocks = soup.select("#floatRightBox > div.js_box.clearfix > div.w_240 > h6")

    if len(owner_sex_blocks[0].find_all("span", class_="member_girl_ico")) > 0:
        sex = "female"
    else:
        sex = "male"

    print("Number: " + str(num))
    print("Title: " + title[0].get_text())
    print("Address: " + address[0].get("title"))
    print("Monthly rental: " + monthly_rental[0].get_text())
    print("Room image: " + room_image[0].get("src"))
    print("Owner image: " + owner_image[0].get("src"))
    print("Owner name: " + owner_name[0].get_text())
    print("Owner sex: " + sex)
    print("-------------------------------------------------------")


if __name__ == '__main__':
    count = 1
    for url in urls:
        for page_url in get_detail_page_url(url):
            if count < max_of_pages:
                analyze_detail_page(page_url, count)
                count += 1
            else:
                break

结果如下:

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/reed/PycharmProjects/web01/web_parse3.py
Number: 1
Title: 北京国贸CBD 近地铁旁精品舒适两居室
Address: 北京市朝阳区百子湾路16号后现代城
Monthly rental: 456
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,32,1048,1800,1200,10b2517f.jpg
Owner image: http://image.xiaozhustatic1.com/21/8,0,48,47,334,334,d6d989f6.jpg
Owner name: 知足小家
Owner sex: female
-------------------------------------------------------
Number: 2
Title: 【两居特价】二环鼓楼大街后海鸟巢温馨两居
Address: 北京市东城区鼓楼大街安德路55号院
Monthly rental: 398
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,79,1519,1800,1200,0af68cad.jpg
Owner image: http://image.xiaozhustatic1.com/21/8,0,61,1483,375,376,8c9f4abd.jpg
Owner name: 天天Tinny
Owner sex: female
-------------------------------------------------------
Number: 3
Title: 市中心地铁旁高端LOFT 可欣赏迷人夜景
Address: 北京市西城区广安门外大街
Monthly rental: 328
Room image: http://image.xiaozhustatic1.com/00,800,533/3,0,33,4595,3000,2000,1f72b9f8.jpg
Owner image: http://image.xiaozhustatic1.com/21/2,0,10,2985,375,375,6999bc2c.jpg
Owner name: 志华
Owner sex: male
-------------------------------------------------------
Number: 4
Title: 紧邻天安门故宫、和平门地铁27号线通透两居
Address: 北京市西城区南新华街18号安平里
Monthly rental: 388
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,11,921,1800,1200,b63ea5d3.jpg
Owner image: http://image.xiaozhustatic1.com/21/8,0,85,2444,281,281,7923cfb2.jpg
Owner name: 大美的家
Owner sex: male
-------------------------------------------------------
Number: 5
Title: 十号线团结湖三里屯太古里一居室艺术气息舒适
Address: 北京市朝阳区东三里屯小区
Monthly rental: 380
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,77,2198,1800,1200,306fba34.jpg
Owner image: http://image.xiaozhustatic1.com/21/8,0,78,1354,363,363,5acd498a.jpg
Owner name: pattypatty
Owner sex: female
-------------------------------------------------------
Number: 6
Title: 创业者之家:国贸CBD豪宅客厅地图间高低床
Address: 北京市朝阳区光华路2号阳光100国际公寓B座
Monthly rental: 89
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,1,1538,1800,1200,06a967e0.jpg
Owner image: http://image.xiaozhustatic1.com/21/7,0,12,14490,332,332,c8d2d49c.jpg
Owner name: 探索者之家
Owner sex: male
-------------------------------------------------------
Number: 7
Title: 九龙山地铁旁舒适一居室
Address: 北京市朝阳区广渠路33号院
Monthly rental: 330
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,55,225,1800,1200,72a214df.jpg
Owner image: http://image.xiaozhustatic1.com/21/8,0,64,2249,364,363,5dd06742.jpg
Owner name: 家有泰迪乖
Owner sex: male
-------------------------------------------------------
Number: 8
Title: 地铁6号线常营长楹天街豪华公寓
Address: 北京市朝阳区6号地铁常营天街豪华公寓
Monthly rental: 197
Room image: http://image.xiaozhustatic1.com/00,800,533/4,0,26,6729,1800,1200,768006fe.jpg
Owner image: http://image.xiaozhustatic1.com/21/3,0,95,5809,359,359,9c0c2802.jpg
Owner name: 爱猪我家萍姐
Owner sex: male
-------------------------------------------------------
Number: 9
Title: 德胜门 后海 积水潭医院 北师大精品2Address: 北京市西城区教场口街9号院
Monthly rental: 378
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,92,2311,1800,1200,54b0e2a3.jpg
Owner image: http://image.xiaozhustatic1.com/21/8,0,95,2273,330,329,0eb20f77.jpg
Owner name: 漫步云中
Owner sex: male
-------------------------------------------------------
Number: 10
Title: 国贸CBD商圈天坛北京南站510号线地铁

Address: 北京市丰台区万科红公寓
Monthly rental: 358
Room image: http://image.xiaozhustatic1.com/00,800,533/3,0,85,5997,1800,1200,56cd3eea.jpg
Owner image: http://image.xiaozhustatic1.com/21/6,0,40,6670,260,260,377c1f3c.jpg
Owner name: simoe陈
Owner sex: male
-------------------------------------------------------
Number: 11
Title: 青年路6号线大悦城阳光充沛温馨舒适的大床房
Address: 北京市朝阳区甘露园中里青年汇佳园8号楼
Monthly rental: 338
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,38,1919,1800,1200,905b258a.jpg
Owner image: http://image.xiaozhustatic1.com/21/8,0,78,1619,329,329,0b2eb4db.jpg
Owner name: 幻想公主
Owner sex: female
-------------------------------------------------------
Number: 12
Title: 【两居特价】新东方 五道口 林大农大两居整租
Address: 北京市海淀区学院路逸城东苑15号楼(林业大学北  五道口商圈北)
Monthly rental: 398
Room image: http://image.xiaozhustatic1.com/00,800,533/6,0,0,4582,1800,1200,f4f3efb7.jpg
Owner image: http://image.xiaozhustatic1.com/21/3,0,13,2417,329,329,957e12fb.jpg
Owner name: 四海为一家
Owner sex: male
-------------------------------------------------------
Number: 13
Title: 【清新】鼓楼后海+胡同独立小院老北京民宿
Address: 北京市东城区鼓楼
Monthly rental: 196
Room image: http://image.xiaozhustatic1.com/00,800,533/7,0,47,11491,1800,1200,855161e4.jpg
Owner image: http://image.xiaozhustatic1.com/21/7,0,97,11132,329,330,90a55b36.jpg
Owner name: 心旅民宿
Owner sex: female
-------------------------------------------------------
Number: 14
Title: 两居72平市中心天安门北京站天坛国贸临铁7Address: 北京市东城区东二环广渠门北里55号
Monthly rental: 500
Room image: http://image.xiaozhustatic1.com/00,800,533/7,0,47,8376,1800,1200,5f37670c.jpg
Owner image: http://image.xiaozhustatic1.com/21/4,0,30,8173,260,260,21e70d52.jpg
Owner name: 美丽的北京你的家
Owner sex: female
-------------------------------------------------------
Number: 15
Title: [特价]学院路 五道口 清华可住5Address: 北京市海淀区学院路逸成东苑22号楼
Monthly rental: 398
Room image: http://image.xiaozhustatic1.com/00,800,533/7,0,69,11545,1800,1200,96423ff4.jpg
Owner image: http://image.xiaozhustatic1.com/21/3,0,13,2417,329,329,957e12fb.jpg
Owner name: 四海为一家
Owner sex: male
-------------------------------------------------------
Number: 16
Title: 六道口、林业大学温馨舒适一居室超值整租
Address: 北京市海淀区学院路逸成东苑19号楼
Monthly rental: 328
Room image: http://image.xiaozhustatic1.com/00,800,533/7,0,14,13043,1800,1200,064c14b3.jpg
Owner image: http://image.xiaozhustatic1.com/21/3,0,13,2417,329,329,957e12fb.jpg
Owner name: 四海为一家
Owner sex: male
-------------------------------------------------------
Number: 17
Title: 特价 后海北师大火箭军医院地铁带阳台主卧
Address: 北京市西城区新德街小区35号院
Monthly rental: 188
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,18,1908,1800,1200,54dae1d1.jpg
Owner image: http://image.xiaozhustatic1.com/21/7,0,41,9114,376,375,100e407c.jpg
Owner name: 薇薇安521
Owner sex: female
-------------------------------------------------------
Number: 18
Title: 国贸双井10号线紧邻地铁口富力城好房出租
Address: 北京市朝阳区双井富力城
Monthly rental: 258
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,57,1658,1800,1200,9694194c.jpg
Owner image: http://image.xiaozhustatic1.com/21/7,0,46,13706,329,329,847cb72f.jpg
Owner name: 大米001
Owner sex: female
-------------------------------------------------------
Number: 19
Title: 地铁五分钟/国贸CBD大望路复式影院音乐主题
Address: 北京市朝阳区后现代城10号楼
Monthly rental: 598
Room image: http://image.xiaozhustatic1.com/00,800,533/7,0,47,11710,1800,1200,c78267e2.jpg
Owner image: http://image.xiaozhustatic1.com/21/7,0,73,10712,363,363,ad6603da.jpg
Owner name: 福态儿哥
Owner sex: male
-------------------------------------------------------
Number: 20
Title: 五道口、清华、林大 典雅带飘窗大两居整租
Address: 北京市海淀区六道口八家嘉园
Monthly rental: 398
Room image: http://image.xiaozhustatic1.com/00,800,533/3,0,80,3627,1800,1200,5af9cd04.jpg
Owner image: http://image.xiaozhustatic1.com/21/3,0,13,2417,329,329,957e12fb.jpg
Owner name: 四海为一家
Owner sex: male
-------------------------------------------------------
Number: 21
Title: 地铁四号线清源路站 (艺术气息温馨大主卧)
Address: 北京市大兴区清源西路
Monthly rental: 138
Room image: http://image.xiaozhustatic1.com/00,800,533/6,0,98,1041,2666,2000,fda9c1a7.jpg
Owner image: http://image.xiaozhustatic1.com/21/3,0,15,339,375,375,742e0af4.jpg
Owner name: 给您一个温暖的家
Owner sex: male
-------------------------------------------------------
Number: 22
Title: 西直门 长河湾 韩式田园温馨2Address: 北京市西城区高粱桥斜街长河湾
Monthly rental: 398
Room image: http://image.xiaozhustatic1.com/00,800,533/1,0,3,5482,825,550,f874984d.jpg
Owner image: http://image.xiaozhustatic1.com/21/1,0,8,5386,333,333,764cfdb4.jpg
Owner name: 豹子楠
Owner sex: female
-------------------------------------------------------
Number: 23
Title: 10号线地铁太阳宫高端公寓进口床垫轻奢大三房
Address: 北京市朝阳区太阳宫半岛国际公寓
Monthly rental: 599
Room image: http://image.xiaozhustatic1.com/00,800,533/7,0,23,12784,1800,1200,1ba52195.jpg
Owner image: http://image.xiaozhustatic1.com/21/7,0,84,10831,329,329,1867ecd6.jpg
Owner name: 快快乐乐的苹果
Owner sex: female
-------------------------------------------------------
Number: 24
Title: 简约大气,浪漫舒适,紧邻9号线科怡路
Address: 北京市丰台区科怡路
Monthly rental: 769
Room image: http://image.xiaozhustatic1.com/00,800,533/2,0,17,728,824,550,f70b7f18.jpg
Owner image: http://image.xiaozhustatic1.com/21/2,0,50,1103,260,260,af7b8829.jpg
Owner name: 三毛的家
Owner sex: female
-------------------------------------------------------
Number: 25
Title: *自由の岛*一居八通线双桥站近传媒大学
Address: 北京市朝阳区八通线城铁双桥站下车走路5分钟东星时尚广场旁
Monthly rental: 285
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,13,1338,1800,1200,558b7578.jpg
Owner image: http://image.xiaozhustatic1.com/21/8,0,97,257,395,395,4373fa1c.jpg
Owner name: W_FullHome
Owner sex: male
-------------------------------------------------------
Number: 26
Title: 六道口地铁五道口鸟巢林大北医清华农大矿大语言
Address: 北京市海淀区学院路学清路城华园小区2号楼
Monthly rental: 328
Room image: http://image.xiaozhustatic1.com/00,800,533/7,0,98,14067,1800,1200,92ca4c9a.jpg
Owner image: http://image.xiaozhustatic1.com/21/7,0,91,13737,329,329,448bdce3.jpg
Owner name: Shadyaftermath
Owner sex: male
-------------------------------------------------------
Number: 27
Title: 市中心两居天安门故宫北京站天坛国贸临铁7Address: 北京市东城区广渠门北里55号
Monthly rental: 500
Room image: http://image.xiaozhustatic1.com/00,800,533/7,0,28,8368,1800,1200,916ca9e8.jpg
Owner image: http://image.xiaozhustatic1.com/21/4,0,30,8173,260,260,21e70d52.jpg
Owner name: 美丽的北京你的家
Owner sex: female
-------------------------------------------------------
Number: 28
Title: 西间壹号--什刹海胡同区独立房屋
Address: 北京市西城区东煤厂胡同11号院
Monthly rental: 360
Room image: http://image.xiaozhustatic1.com/00,800,533/8,0,41,1748,1800,1200,ea1312a4.jpg
Owner image: http://image.xiaozhustatic1.com/21/7,0,91,13673,329,329,14c1ce41.jpg
Owner name: 堂屋
Owner sex: female
-------------------------------------------------------
Number: 29
Title: 舒适潘家园十里河地铁1014号朝阳妇幼国贸
Address: 北京市朝阳区十里河桥西宏善家园
Monthly rental: 268
Room image: http://image.xiaozhustatic1.com/00,800,533/6,0,20,3419,1800,1200,b1296bcc.jpg
Owner image: http://image.xiaozhustatic1.com/21/4,0,91,8102,329,329,2583e316.jpg
Owner name: 兰絮的家
Owner sex: female
-------------------------------------------------------

Process finished with exit code 0

遗留问题,两个image的地址好像是无效的,本地无法打开。

发布了45 篇原创文章 · 获赞 6 · 访问量 3万+
展开阅读全文

求大佬解释python的代码,期末作业太难了,主要是辅助函数那里都看不懂

06-19

import turtle as tur from math import sin, cos, pi DRAW_SPEED = 3 PEN_SIZE = 6 WIDTH = 600 HEIGHT = 600 """ 辅助函数 """ def linear_interp(p1, p2, t): return p1 * (1 - t) + p2 * t def bezier1(p1, p2, t): return ( linear_interp(p1[0], p2[0], t), linear_interp(p1[1], p2[1], t) ) def bezier2(p1, p2, p3, t): return bezier1( bezier1(p1, p2, t), bezier1(p2, p3, t), t ) def bezier3(p1, p2, p3, p4, t): return bezier1( bezier2(p1, p2, p3, t), bezier2(p2, p3, p4, t), t ) def bezier_3(p1, p2, p3, p4): for t in [x / 15 for x in range(0, 15 + 1)]: x, y = bezier3(p1, p2, p3, p4, t) tur.goto(x, y) def p(c): return complex( + c.real - WIDTH / 2, - c.imag + HEIGHT / 2 ) def M(c): tur.goto(p(c).real, p(c).imag) def L(start, end): M(start) M(end) def C(start, control1, control2, end): M(start) bezier_3( (p(start).real, p(start).imag), (p(control1).real, p(control1).imag), (p(control2).real, p(control2).imag), (p(end).real, p(end).imag)) def f(sp=0j, rgb='red'): tur.fillcolor(rgb) M(sp) tur.begin_fill() nf = tur.end_fill def pd(sp, rgb="#E4007F", bold=PEN_SIZE): tur.color(rgb) tur.pensize(bold) M(sp) tur.pendown() def pu(): tur.penup() tur.pensize(PEN_SIZE) tur.color("black") def E( cx, cy, a, b, matrix=(1, 0, 0, 1, 0, 0)): cp = complex(cx, cy) sp = cp + a tur.penup() M(transform(sp, matrix)) tur.color("#FB8D8C") tur.pensize(0.1) tur.fillcolor("#FB8D8C") tur.begin_fill() tur.pendown() for i in range(0, 360 + 1): x = a * sin(i * 2 * pi / 360 + pi / 2) + cx y = b * cos(i * 2 * pi / 360 + pi / 2) + cy p = complex(x, y) M(transform(p, matrix)) nf() pu() def transform(p, matrix=(1, 0, 0, 1, 0, 0)): a, b, c = matrix[0], matrix[1], matrix[2], d, e, f = matrix[3], matrix[4], matrix[5], return complex( a * p.real + c * p.imag + e, b * p.real + d * p.imag + f ) def Mv(c): tur.penup() tur.goto(p(c).real, p(c).imag) tur.pendown() """ 绘图代码 """ screen = tur.Screen() screen.bgcolor("white") tur.tracer(2) tur.pensize(PEN_SIZE) tur.speed(DRAW_SPEED) tur.penup() ## 右眼 pd(252.7+228.84j, rgb="#29ABE2", bold=6) # 右眼-pd f(252.7+228.84j, "#29ABE2") C(252.7+228.84j, 261.37+225.51j, 270.03+222.17j, 278.7+218.84j) C(278.7+218.84j, 284.05+216.78j, 281.73+208.08j, 276.31+210.16j) C(276.31+210.16j, 267.64+213.49j, 258.98+216.83j, 250.31+220.16j) C(250.31+220.16j, 244.95+222.22j, 247.28+230.92j, 252.7+228.84j) L(252.7+228.84j, 252.7+228.84j) nf() 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览