python 爬取所有页面的对应数据

一般来说不同页码最后page=或者p  等等,只需要转化一下后面对应的数值即可,或者从尾页对应URL找到最后一页,也就是总页数即可

案例一:

#!/usr/bin/env python

# -*- coding: utf-8 -*-
import pymysql  # 导入 pymysql
import re
import time
import datetime
import requests
import string
from lxml import etree
# 打开数据库连接
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
}
response = requests.get('https://www.wdzj.com/dangan/search?filter=e3',headers=headers)
r = response.text
html = etree.HTML(r,etree.HTMLParser())
s = html.xpath('//*[@id="showTable"]/div/div//@currentnum')
total_page = int(s[-1])
for i in range(1,total_page+1):
    final_url = 'https://www.wdzj.com/dangan/search?filter=e3&currentPage='+str(i)
    response = requests.get(final_url, headers=headers)
    r = response.text
    html = etree.HTML(r, etree.HTMLParser())
    r1 = html.xpath('//h2/a')
    r2 = html.xpath('//h2/div[@class="itemTag"]')
    r3 = html.xpath('//a[@class="itemConLeft"]/div[@class="itemConBox"][3]')
    r4 = html.xpath('//a[@class="itemConLeft"]/div[@class="itemConBox bgBox"]/strong')
    for j in range(25):
        name = r1[j].xpath('string(.)')
        problem = r2[j].xpath('string(.)')
        address = r3[j].xpath('string(.)')
        score = r4[j].xpath('string(.)')
        print(name,problem.strip(),address,"综合评分:",score)
        

案例二:

#!/usr/bin/env python

# -*- coding: utf-8 -*-
import pymysql  # 导入 pymysql
import re
import time
import datetime
import requests
from lxml import etree
import string
from bs4 import BeautifulSoup
# 打开数据库连接
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
}
response = requests.get('https://www.p2peye.com/platform/zonghe/',headers=headers)
r = response.text
html = etree.HTML(r, etree.HTMLParser())
s = html.xpath('//*[@id="nv_platform"]/div[14]/div/div[1]/div/div/div[3]/div/div/a[7]/@href')
s[0] = str(s[0])
s[0] = re.sub("\D",'',s[0])
total_page = int(s[0])
for  i in range(1,total_page+1):
    final_url = 'https://www.p2peye.com/platform/zonghe/p'+str(i)
    response = requests.get(final_url,headers=headers)
    r = response.text
    html = etree.HTML(r,etree.HTMLParser())
    r0 = html.xpath('//td[1]')
    r1 = html.xpath('//td[2]/a[@class="color-blue"]')
    r2 = html.xpath('//tbody//td[3]')
    r3 = html.xpath('//tbody//td[2]/a/@href')
    for j in range(40):
        rank = r0[j].xpath('string(.)')
        name = r1[j].xpath('string(.)')
        score = r2[j].xpath('string(.)')
        url = r3[j]
        print(rank, name, score, url)
        

 

好的,可以给你一个大致的思路: 1. 首先,你需要用 Python 的 requests 库向目标网站发送请求,获取对应的 JSON 数据。 2. 接着,你需要使用 Python 的 json 库将 JSON 数据转换成 Python 对象,方便后续处理。如果 JSON 数据比较大,你可以使用 ijson 库来逐行读取并解析 JSON 数据,可以节省内存空间。 3. 接下来,你需要连接到你的数据库,并且在其中创建一个表,用来存储你要插入的数据。你可以使用 Python 的 MySQLdb 或者 PyMySQL 等库来连接 MySQL 数据库。 4. 然后,你需要将 Python 对象中的数据逐行插入到数据库中对应的表中。你可以使用 SQL 语句来实现,比如 INSERT INTO 语句。 下面是一个简单的示例代码,可以帮助你理解上述的思路: ```python import requests import json import pymysql # 发送请求,获取 JSON 数据 url = "https://example.com/api/data" response = requests.get(url) data = json.loads(response.text) # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', db='test') cursor = conn.cursor() # 创建表 sql_create_table = ''' CREATE TABLE IF NOT EXISTS `data` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) DEFAULT NULL, `value` FLOAT DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ''' cursor.execute(sql_create_table) # 插入数据 for item in data: name = item['name'] value = item['value'] sql_insert = f"INSERT INTO `data` (`name`, `value`) VALUES ('{name}', {value})" cursor.execute(sql_insert) # 提交事务并关闭连接 conn.commit() cursor.close() conn.close() ``` 当然,这只是一个简单的示例代码,实际上在实际应用中,你还需要考虑一些异常处理、错误处理、数据清洗等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值