ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

学习python爬虫笔记1----豆瓣TOP250

2021-07-31 14:02:20  阅读:244  来源: 互联网

标签:group python resp dic ---- nbsp strip print TOP250


# Author:KRL
# -*- codeing = utf-8 -*-
# @Time :2021/7/3020:13
# @Author :MI
# @Site :
# @File :doubantop250.py
# @Software :PyCharm

# 拿到网页源代码 requests
# 利用re提取我们需要的内容 re
import requests
import re
import csv



url = "https://movie.douban.com/top250"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}

resp = requests.get(url,headers=headers)
page_content = resp.text
# print(resp.text)

# 解析数据
obj = re.compile(r'<li>.*? <div class="item">.*?<span class="title">(?P<name>.*?)</span>.*?'
r'<p class="">(?P<director>.*?);&nbsp;&nbsp;(?P<actors>.*?)<br>(?P<years>.*?)'
r'&nbsp;/&nbsp;(?P<country>.*?)&nbsp;/&nbsp;(?P<style>.*?)</p>.*?'
r'<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?'
r'<span>(?P<Number_of_comments>.*?)</span>.*?<span class="inq">(?P<motto>.*?)</span>',re.S)

# 开始匹配
result = obj.finditer(page_content)

# 写入CSV 为数据分析做准备
f = open("data.csv",mode="w",encoding='utf-8')
csvwriter = csv.writer(f)

for it in result:
# print(it.group("name"))
# print(it.group("director").strip())
# print(it.group("actors"))
# print(it.group("years").strip())
# print(it.group("country"))
# print(it.group("style").strip())
# print(it.group("score").strip())
# print(it.group("Number_of_comments"))
# print(it.group("motto"))
dic = it.groupdict()
dic['director'] = dic['director'].strip()
dic['years'] = dic['years'].strip()
dic['style'] = dic['style'].strip()
dic['score'] = dic['score'].strip()
csvwriter.writerow(dic.values())

f.close()
print('done!')

标签:group,python,resp,dic,----,nbsp,strip,print,TOP250
来源: https://www.cnblogs.com/krlking/p/15084097.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有