hirgnz 发表于 2020-10-1 21:56

十万多rss源爬虫

如题,最近喜欢上折腾rss,就差rss订阅源。最近新上架了一个应用,抓包接口,从里面爬取一些rss源。没时间去学python,但是又想去尝试写,结果多线程写出来的比java慢很多,就放单线程代码上来吧。感兴趣的也可以帮忙修改下放评论区(随机user-agent+代{过}{滤}理ip),不然十万多条数据,估计给封ip接口从1到十万多,因为中间有些rss是失效的,被删除所以没数据。我是用count进行判断的,连续30次获取到的数据都为空即退出。附上代码;import requests
import json
import csv

rows = []
num = 3000
count = 0
headers = {
      'Host': 'api.qingmang.me',
      'Connection': 'close',
      'User-Agent': 'Mango 3.0.0 (iPhone 6s Plus; ios-iphone 13.3.1; zh_CN)'
    }

for n in range(0, 500):
    url = 'http://api.qingmang.me/v2/pool.list.info?list=k'
    num = num + 1
    url = url + str(num)
    row = []
    # 打印本次请求地址
    print(url)

    # 发送请求,获取响应结果
    response = requests.get(url=url, headers=headers, verify=False)
    text = response.text

    # 将响应内容转换为Json对象
    res_json = json.loads(text)
    if str(res_json['ok']) == 'True':
      count = 0
      # 从Json对象获取想要的内容
      temp = res_json['listInfo']
      if 'listId' in temp:
            id = temp['listId']
      else:
            id = ''
      if 'contentType' in temp:
            contentType = temp['contentType']
      else:
            contentType = ''
      if 'name' in temp:
            name = temp['name']
      else:
            name = ''
      if 'rssUrl' in temp:
            rssUrl = temp['rssUrl']
      else:
            rssUrl = ''
      if 'sourceUrl' in temp:
            sourceUrl = temp['sourceUrl']
      else:
            sourceUrl = ''
      # 生成行数据
      row.append(str(id))
      row.append(str(contentType))
      row.append(str(name))
      row.append(str(rssUrl))
      row.append(str(sourceUrl))

      # 保存行数据
      rows.append(row)

    else:
      print("该节点没有rss")
      print(count)
      count = count + 1
      # 由于接口部分rss源失效,因此判定连续30次没有获取数据即退出爬取
      if count > 30:
            print("已经到尽头了,最后id为:k" + str(num))
            break

# 生成Excel表头
header = ['Id','类型','名称','rss订阅源地址','原始地址']

# 将表头数据和爬虫数据导出到Excel文件
with open('D:\\rss.csv', 'w', encoding='gb18030',newline='') as f:
    f_csv = csv.writer(f)
    f_csv.writerow(header)
    f_csv.writerows(rows)

761326862 发表于 2020-10-1 21:57

6666666666

1415759153 发表于 2020-10-1 22:04

谢谢大牛

1257787137 发表于 2020-10-1 22:54

谢谢分享

17743005111 发表于 2020-10-1 23:01

6666

haihai 发表于 2020-10-1 23:26

感谢楼主分享

bzpzmmsl 发表于 2020-10-1 23:51

谢谢大佬

machuanfeng 发表于 2020-10-2 03:40

好的,非常感谢
页: [1]
查看完整版本: 十万多rss源爬虫