第一阶段冲刺(三)

日期:2021.05.05

作者:杨传伟

完成任务:学习爬虫的xpath解析,使用xpath解析爱奇艺网站电影片库首页电影数据(电影名字、播放链接、评分、播放权限)并存到数据库。

爬虫源码:

复制代码
 1 url='https://list.iqiyi.com/www/1/-------------11-1-1-iqiyi--.html'
 2     headers={
 3         'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
 4                      'Chrome/90.0.4430.93 Safari/537.36'
 5     }
 6     #获取爱奇艺电影大全主界面response
 7     response=requests.get(url=url,headers=headers)
 8     response.encoding='utf-8'
 9     #获取页面的全部html
10     page_text=response.text
11     # html=BeautifulSoup(page_text,"lxml")
12     # bs_li=html.find_all('li',class_="qy-mod-li")
13     # print(bs_li)
14     #打印输出页面html
15     # print(page_text)
16     #实例化xpath对象
17     etree_=etree.HTML(page_text)
18     #获取电影的所有li标签
19     ul_list=etree_.xpath('//ul[@class="qy-mod-ul"]/li')
20     # print(ul_list[0])
21     temp_list=[]        #声明一个list存储单部电影的所有信息
22     dataRes=[]         #声明一个总list存储所有的电影
23     #BeautifulSoup 解析播放状态
24     # findState=re.compile(r'"<img src="(.*?)"')
25     # for li in bs_li:
26     #     words=str(li)
27     #     print(words)
28     #     temp_state=re.findall(findState,words)
29     #     print(temp_state)
30 
31     for li in ul_list:
32         name=li.xpath('./div/div[2]/p[1]/a/@title')     #获取电影名字
33         score = li.xpath('./div/div[2]/p[1]/span/text()')  # 获取电影评分
34         link=li.xpath('./div/div[2]/p[1]/a/@href')     #获取电影链接
35         if(len(score)==0):                                  #如果评分信息没有 重新赋值
36             score="暂无评分"
37         if (len(link) == 0):                            # 如果链接信息没有 重新赋值
38             score = "#"
39         link=str.replace(link[0],"//","")           #因为链接是带有 这样 // 的两个斜杠 所以要替换一下
40         #解析播放状态
41         #//*[@id="block-D"]/ul/li[5]/div/div[1]/a/div[2]/img
42         state=li.xpath('./div/div[1]/a/div[2]/img/@src')
43         # print(state)
44         temp_list.append(name[0])
45         temp_list.append(score[0])
46         temp_list.append(link)
47 
48         # print(temp_list)
49         dataRes.append(temp_list)       #把爬取到的每一部电影存到总list中也就是 dataRes
50         temp_list=[]        #给单部电影list重新赋值为空list
51     print(dataRes)
复制代码

截图示例:


5.5 李楠

今日初步实现了手机端页面的前端,主要分为首页:将来显示推荐电影,分类:将来分类查找电影,我的:查看个人信息以及收藏内容。三个页面实现了底部导航栏切换与滑屏切换,使用了Fragment与viewpager实现。

主要代码:

Xml部分:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context=".MainActivity">
 8 
 9 
10     <RadioGroup
11         android:id="@+id/main_group"
12         android:layout_width="match_parent"
13         android:layout_height="56dp"
14         android:layout_alignParentBottom="true"
15         android:background="#ffffff"
16         android:orientation="horizontal">
17 
18         <RadioButton
19             android:id="@+id/main_rb_first"
20             style="@style/tab_menu_item"
21             android:drawableTop="@drawable/firstpage"
22             android:text="首页" />
23 
24         <RadioButton
25             android:id="@+id/main_rb_cloud"
26             style="@style/tab_menu_item"
27             android:drawableTop="@drawable/cloudpage"
28             android:text="分类" />
29 
30         <RadioButton
31             android:id="@+id/main_rb_my"
32             style="@style/tab_menu_item"
33             android:drawableTop="@drawable/mypage"
34             android:text="我的" />
35 
36 
37     </RadioGroup>
38 
39     <View
40         android:id="@+id/div_tab_bar"
41         android:layout_width="match_parent"
42         android:layout_height="2px"
43         android:layout_above="@id/main_group"
44         android:background="#DFDBDB" />
45 
46     <androidx.viewpager.widget.ViewPager
47         android:id="@+id/vpager"
48         android:layout_width="match_parent"
49         android:layout_height="match_parent"
50         android:layout_above="@id/div_tab_bar"/>
51 
52 </RelativeLayout>
复制代码

MyFragmentPagerAdapter:

复制代码
 1 package com.example.cloudlibrary.Adapter;
 2 
 3 import android.view.ViewGroup;
 4 
 5 import androidx.fragment.app.Fragment;
 6 import androidx.fragment.app.FragmentManager;
 7 import androidx.fragment.app.FragmentPagerAdapter;
 8 
 9 import com.example.cloudlibrary.Fragment.CloudPageFragment;
10 import com.example.cloudlibrary.Fragment.FirstPageFragment;
11 import com.example.cloudlibrary.Fragment.MyPageFragment;
12 import com.example.cloudlibrary.MainActivity;
13 
14 public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
15     private final int PAGER_COUNT = 3;
16     private CloudPageFragment cloudPageFragment = null;
17     private FirstPageFragment firstPageFragment = null;
18     private MyPageFragment myPageFragment = null;
19 
20 
21     public MyFragmentPagerAdapter(FragmentManager fm) {
22         super(fm);
23         cloudPageFragment = new CloudPageFragment();
24         firstPageFragment = new FirstPageFragment();
25         myPageFragment = new MyPageFragment();
26     }
27 
28 
29     @Override
30     public int getCount() {
31         return PAGER_COUNT;
32     }
33 
34     @Override
35     public Object instantiateItem(ViewGroup vg, int position) {
36         return super.instantiateItem(vg, position);
37     }
38 
39     @Override
40     public void destroyItem(ViewGroup container, int position, Object object) {
41         System.out.println("position Destory" + position);
42         super.destroyItem(container, position, object);
43     }
44 
45     @Override
46     public Fragment getItem(int position) {
47         Fragment fragment = null;
48         switch (position) {
49             case MainActivity.PAGE_ONE:
50                 fragment = firstPageFragment;
51                 break;
52             case MainActivity.PAGE_TWO:
53                 fragment = cloudPageFragment;
54                 break;
55             case MainActivity.PAGE_THREE:
56                 fragment = myPageFragment;
57                 break;
58         }
59         return fragment;
60     }
61 
62 }
复制代码
每个页面的Fragment都是类似的,目前没有实际内容只返回了一个页面:
复制代码
 1 package com.example.cloudlibrary.Fragment;
 2 
 3 import androidx.annotation.NonNull;
 4 import androidx.annotation.Nullable;
 5 import androidx.appcompat.app.AppCompatActivity;
 6 import androidx.fragment.app.Fragment;
 7 
 8 import android.os.Bundle;
 9 import android.view.LayoutInflater;
10 import android.view.View;
11 import android.view.ViewGroup;
12 
13 import com.example.cloudlibrary.R;
14 
15 public class FirstPageFragment extends Fragment {
16 
17     public FirstPageFragment(){
18     }
19     @Override
20     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
21         View view = inflater.inflate(R.layout.activity_first_page, container, false);
22         return view;
23     }
24 }
复制代码


5.5 章英杰

任务进度:完成了页面“电影TOP榜”和广告分块部分。其中“电影TOP榜”部分的每一个电影模块也分为了两个div模块——

电影海报部分和电影详情部分,

在广告部分中,每一个广告可设为一个div模块。

产品页面:

任务看板

原文地址:https://www.cnblogs.com/zyj3955/p/14747525.html