小白自学爬虫项目案例实战——Reqeusts + PyQuery/Re + PyMongo/Json豆瓣电影TOP250

本文介绍了一个小白自学爬虫的实战项目,通过Python的Reqeusts、PyQuery/Re、PyMongo和Json库抓取并存储豆瓣电影TOP250的数据。在分析网站结构后,编写了爬虫代码。然而,由于请求频率过高和IP未完全匿名,导致请求被限制。为模拟人为操作,代码中加入了延迟策略,但最终因IP被封而停止了进一步的研究。
摘要由CSDN通过智能技术生成

分析网站

首先打开谷歌浏览器,F12打开开发者调试工具

代码

话不多说,直接上代码

import json
import random
import time
import requests
import logging
import re
import pymongo
from pyquery import PyQuery as pq
import urllib
import multiprocessing
'''
requests  			用来爬取页面,
logging   			用来输出信息,
re        			用来实现正则表达式解析,
pyquery   			用来直接解析网页,
pymongo   			用来实现MongoDB存储,
multiprocessing     采用多线程来优化
'''
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(levelname)s: %(message)s')
ip = 'http://xxxxxx'
def api(thisapi):
    count=0
    while count<10:
        try:
            urllib.request.urlcleanup()
            thisip = urllib.request.urlopen(thisapi).read().decode("utf-8", "ignore")
            print("当前用的ip是:" + thisip)
            thisip = re.findall(r'([0-9\.:]+)', thisip)[0]
            if thisip:
                proxy_temp = {
   "http": "http://" + thisip}
                return proxy_temp
        except Exception as e:
            #time.sleep(3)
            count+=1
            print("代理ip无效!正在第{}次重新切换中...".format(count))
            continue

headers={
   
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
}

def get_url(url):
    proxy=api(ip)
    count=0
    while
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值