python 爬取mm信息

本文介绍了一种使用Python爬取淘宝模特基本信息的方法。通过发送HTTP请求获取网页内容,并利用BeautifulSoup解析网页,提取模特的名字、年龄、学校等详细信息。
# -*- coding:utf-8 -*-
 
import requests
from bs4 import BeautifulSoup
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')


for num in range(1,4300):
    try:
        URL = 'http://mm.taobao.com/json/request_top_list.htm?page=%d' % num
        #print "现在爬取的网站url是:" + URL
        response = requests.get(URL) 
        response.encoding = 'gb2312'
        text = response.text 
        soup = BeautifulSoup(text, 'lxml') 
        for model in soup.select(".list-item"):
            try:
                model_id =  model.find('span', {'class': 'friend-follow J_FriendFollow'})['data-userid']
                json_url = "http://mm.taobao.com/self/info/model_info_show.htm?user_id=%d" % int(model_id)
                response_json = requests.get(json_url)
                response_json.encoding = 'gb2312'
                text_response_json = response_json.text
                soup_json = BeautifulSoup(text_response_json, 'lxml')
                
                print "***********************************" + model.find('a', {'class': 'lady-name'}).string + "*********************************"
                print "模特的名字:" + model.find('a', {'class': 'lady-name'}).string
                print "模特的年龄:"+ model.find('p', {'class': 'top'}).em.strong.string
                print "生日:" + soup_json.find('li', {'class': 'mm-p-cell-left'}).span.string
                blood =  soup_json.find_all('li', {'class': 'mm-p-cell-right'})[1].span.string
                if blood is None:
                    blood = "无"
                print "血型:" + blood
                print "学校/专业:" + soup_json.find_all('li')[5].span.string
                print "身高:" + soup_json.find('li', {'class': 'mm-p-small-cell mm-p-height'}).p.string
                print "体重:" + soup_json.find('li', {'class': 'mm-p-small-cell mm-p-weight'}).p.string
                print "三围:" + soup_json.find('li', {'class': 'mm-p-small-cell mm-p-size'}).p.string
                print "罩杯:" + soup_json.find('li', {'class': 'mm-p-small-cell mm-p-bar'}).p.string
                print "鞋码:" + soup_json.find('li', {'class': 'mm-p-small-cell mm-p-shose'}).p.string
                print "模特所在地:"+ model.find('p', {'class': 'top'}).span.string
                print "模特的id:"+ model.find('span', {'class': 'friend-follow J_FriendFollow'})['data-userid']
                print "模特的标签:"+ model.find_all('p')[1].em.string
                print "模特的粉丝数:"+ model.find_all('p')[1].strong.string
                print "模特的排名:"+ [text for text in model.find('div', {'class': 'popularity'}).dl.dt.stripped_strings][0]
                print model.find('ul', {'class': 'info-detail'}).get_text(" ",strip=True)
                print "模特的个人资料页面:" +"http:"+ model.find('a', {'class': 'lady-name'})['href']			             		
                print "模特的个人作品页面:" +"http:"+ model.find('a', {'class': 'lady-avatar'})['href']
                print "模特的个人头像:" + "http:" + model.find('img')['src']
                print "***********************************" + model.find('a', {'class': 'lady-name'}).string + "*********************************"
                print "\n"
		
            except:
                print "error"
    except:
        print num + "page is error"


### 使用 Python 抓取携程网页上的航班票价数据 为了实现这一目标,可以采用 `requests` 和 `BeautifulSoup` 库来处理 HTTP 请求并解析 HTML 文档。需要注意的是,在实际操作前应当阅读目标网站的服务条款,确保虫行为合法合规。 #### 准备工作 安装必要的库可以通过 pip 完成: ```bash pip install requests beautifulsoup4 lxml ``` #### 获取页面内容 构建请求头模拟浏览器访问,并发送 GET 请求到指定 URL 来获取包含航班列表的网页源码[^1]。 ```python import requests from bs4 import BeautifulSoup headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" } def fetch_page_content(url): try: response = requests.get(url, headers=headers) if response.status_code == 200: return response.text else: print(f"Failed to retrieve page content with status code {response.status_code}") return None except Exception as e: print(e) return None ``` #### 解析HTML文档提取所需信息 利用 BeautifulSoup 对返回的数据进行分析,定位到存储有航班详情的部分,进而抽取具体字段如价格、航空公司名称等[^2]。 ```python def parse_flight_data(html_content): soup = BeautifulSoup(html_content, 'lxml') flights_info = [] items = soup.find_all('div', class_='search-item') # 假设这是保存单个航班记录的选择器 for item in items: flight_details = {} price_tag = item.select_one('.price') # 替换成真实的CSS选择器路径 airline_name_tag = item.select_one('.airline-name') # 同上 if not (price_tag and airline_name_tag): # 如果找不到对应标签则跳过当前项 continue flight_details['Price'] = price_tag.string.strip() flight_details['Airline Name'] = airline_name_tag.string.strip() flights_info.append(flight_details) return flights_info ``` #### 主函数逻辑组合上述功能模块 最后定义一个主流程控制函数用于串联起前面编写的各个部分,输入参数为出发城市代码与到达城市代码以及日期字符串形式的时间戳,输出则是整理后的航班报价清单[^3]。 ```python if __name__ == "__main__": departure_city = input("请输入出发城市的三字代码:") destination_city = input("请输入目的城市的三字代码:") date_str = input("请输入出行日期(格式YYYY-MM-DD):") url_template = f"http://flights.ctrip.com/booking/{departure_city}-day-1.html?DDate1={date_str}" html_text = fetch_page_content(url_template) if html_text is not None: result_list = parse_flight_data(html_text) for idx, info_dict in enumerate(result_list, start=1): print(f"{idx}. 航班:{info_dict['Airline Name']} | 价格:CNY{info_dict['Price']}") else: print("未能成功加载网页,请稍后再试或检查网络连接.") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值