Python web crawler(5)多页网站拼接

先搞单页网站:

import requests
from lxml import etree
import re

url = 'https://*********.com/top250?start=1'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'Cookie': '3',
}
response = requests.get(url, headers=headers)
data = response.content.decode()

tree = etree.HTML(data)

div_list = tree.xpath('//div[@class="hd"]')
# print(div_list)
for div in div_list:
    title = re.sub('\s', '', ''.join(div.xpath('.//text()')))
    print(title)
    
    
# 执行结果
霸王别姬/再见,我的妾/FarewellMyConcubine[可播放]
阿甘正传/ForrestGump/福雷斯特·冈普[可播放]
泰坦尼克号/Titanic/铁达尼号(港/台)[可播放]
这个杀手不太冷/Léon/终极追杀令(台)/杀手莱昂[可播放]
千与千寻/千と千尋の神隠し/神隐少女(台)/千与千寻的神隐
美丽人生/Lavitaèbella/一个快乐的传说(港)/LifeIsBeautiful[可播放]
星际穿越/Interstellar/星际启示录(港)/星际效应(台)[可播放]

多页分析:

我们获取几套页面,对页面URL进行比对

第1页 = 'https://*********.com/top250?start=1'
第2页 = 'https://*********.com/top250?start=25'
第3页 = 'https://*********.com/top250?start=50'
第4页 = 'https://*********.com/top250?start=75'

我们发现一个共性,那就是url中的start=后面是以25的倍数增加的,并且规律是页数(n-1)*25

最后我们尝试将第一页的start=1改成start=0,效果完全不变!

第1页 = 'https://*********.com/top250?start=1'
第1页 = 'https://*********.com/top250?start=0'

因此我们可以获得一个网页拼接方案

for i in range (1,11)
    count = (i - 1) * 25 # 计算从多少开始取值
    url = 'https://*********.com/top250?start='+str(count)
    print(url)

或者是

for i in range(1, 11):
    count = (i - 1) * 25 # 计算从多少开始取值
    url = f'https://*********.com/top250?start={count}'
    print(url)

执行结果是

https://*********.com/top250?start=0
https://*********.com/top250?start=25
https://*********.com/top250?start=50
https://*********.com/top250?start=75
https://*********.com/top250?start=100
https://*********.com/top250?start=125
https://*********.com/top250?start=150
https://*********.com/top250?start=175
https://*********.com/top250?start=200
https://*********.com/top250?start=225

记得每次循环后增加睡眠时间,以防止被拉黑

import random
import time

# 自省时间 防止访问过快被拉黑
time.sleep(random.randint(1, 4))

 

搞多页网站

import random
import re
import requests
import time
from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'Cookie': '3',
}

for i in range(1, 11):
    count = (i - 1) * 25 # 计算从多少开始取值
    url = f'https://*********.com/top250?start={count}'
    print(url)
    
    response = requests.get(url, headers=headers)
    data = response.content.decode()
    tree = etree.HTML(data)
    
    div_list = tree.xpath('//div[@class="hd"]')
    
    for div in div_list:
        title = re.sub('\s', '', ''.join(div.xpath('.//text()')))
        print(title)
    # 自省时间 防止访问过快被拉黑
    time.sleep(random.randint(3, 7))
    
    
# 执行结果
https://*********.com/top250?start=75
霸王别姬/再见,我的妾/FarewellMyConcubine[可播放]
阿甘正传/ForrestGump/福雷斯特·冈普[可播放]
泰坦尼克号/Titanic/铁达尼号(港/台)[可播放]
https://*********.com/top250?start=100
这个杀手不太冷/Léon/终极追杀令(台)/杀手莱昂[可播放]
千与千寻/千と千尋の神隠し/神隐少女(台)/千与千寻的神隐
美丽人生/Lavitaèbella/一个快乐的传说(港)/LifeIsBeautiful[可播放]
星际穿越/Interstellar/星际启示录(港)/星际效应(台)[可播放]

 

posted @ 2024-01-22 18:57  Magiclala  阅读(12)  评论(0编辑  收藏  举报