Python 爬虫之第一次接触

爬豆瓣网电影TOP250名单 ------- 代码未写完,等待更新

 

import requests
from requests.exceptions import RequestException
import re
import json

def get_one_page(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:#判断网页是否正常开启
            return response.text#返回没有
        return None
    except RequestException:
        return None

def parse_one_page(html):
    pattern = re.compile("", re.S)#笔记本屏幕太小看不过来
    items = re.findall(pattern, html)

    for item in items:#把爬取的信息进行格式化
        yield{
            "":item[0],
            "":item[1],
        }


def write_to_file(content):
    with open("result.txt", "a", encoding="utf-8") as f:
        f.write(json.dumps(content, ensure_ascii=False) + "\n")
        f.close()

def main():
    url = "https://movie.douban.com/top250"
    html = get_one_page(url)
    # parse_one_page(html)
    for item in parse_one_page(html):
        print(item)
        write_to_file(item)

if __name__ == "__main__":
    main()

#这段代码没有加RE的匹配方式(笔记本屏幕小)
#第二个这段代码只能匹配一页的东西,自己发挥想象把

 

转载于:https://www.cnblogs.com/GhostCatcg/p/8099660.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值