鱼C论坛

 找回密码
 立即注册
查看: 1693|回复: 3

[已解决]某站批量爬取的问题

[复制链接]
发表于 2020-9-27 09:38:36 | 显示全部楼层 |阅读模式
10鱼币
目前实现了单个页面的爬取,代码如下:
  1. import requests
  2. from bs4 import BeautifulSoup

  3. headers = {
  4. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
  5. }
  6. r = requests.get('https://site.ip138.com/192.168.5.6/',headers=headers)
  7. html = r.text
  8. soup = BeautifulSoup(html,'lxml')
  9. for ul in soup.find_all(attrs={'id':'list'}):
  10.     for a in ul.find_all(name='a'):
  11.         print(a.string)
复制代码

现在有一个ip.txt,里面10个ip地址,需要返回这10个ip地址的查询内容并将结果输出为txt(每个ip替换192.168.5.6)
最佳答案
2020-9-27 09:38:37
是要这样子?

  1. import requests
  2. from bs4 import BeautifulSoup

  3. headers = {
  4.     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
  5. }
  6. iplist = ['192.168.5.6', '192.168.5.5', '119.75.217.109']


  7. for i in iplist:
  8.     url = f'https://site.ip138.com/{i}/'
  9.     print(url)
  10.     r = requests.get(url=url, headers=headers)
  11.     html = r.text
  12.     soup = BeautifulSoup(html, 'lxml')
  13.     with open('ip.txt',mode='w',encoding='utf-8') as f:
  14.         for ul in soup.find_all(attrs={'id': 'list'}):
  15.             for a in ul.find_all(name='a'):
  16.                 print(a.string)
  17.                 f.write(a.string)
  18.                 f.write('\n')
复制代码

最佳答案

查看完整内容

是要这样子?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-27 09:38:37 | 显示全部楼层    本楼为最佳答案   
是要这样子?

  1. import requests
  2. from bs4 import BeautifulSoup

  3. headers = {
  4.     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
  5. }
  6. iplist = ['192.168.5.6', '192.168.5.5', '119.75.217.109']


  7. for i in iplist:
  8.     url = f'https://site.ip138.com/{i}/'
  9.     print(url)
  10.     r = requests.get(url=url, headers=headers)
  11.     html = r.text
  12.     soup = BeautifulSoup(html, 'lxml')
  13.     with open('ip.txt',mode='w',encoding='utf-8') as f:
  14.         for ul in soup.find_all(attrs={'id': 'list'}):
  15.             for a in ul.find_all(name='a'):
  16.                 print(a.string)
  17.                 f.write(a.string)
  18.                 f.write('\n')
复制代码
屏幕截图 2020-09-27 112558.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-27 09:53:36 | 显示全部楼层
  1. import requests
  2. from bs4 import BeautifulSoup


  3. if __name__ == '__main__':
  4.     headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}
  5.     base_url = 'https://site.ip138.com/'
  6.     with open('ip.txt') as f:  # 因为不知道你的ip.txt的内容,假定每行1个ip,其他情况你自己改改代码。
  7.         for ip in f.readlines():
  8.             url = base_url + ip
  9.             r = requests.get(url,headers=headers)
  10.             soup = BeautifulSoup(r.text, 'lxml')
  11.             with open('result.txt', 'a') as o:
  12.                 for ul in soup.find_all(attrs={'id':'list'}):
  13.                     for a in ul.find_all(name='a'):
  14.                         o.write(a.string)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-9-27 10:23:35 | 显示全部楼层
您好,一个ip如果对应多个域名,单个ip爬取输出多个域名 微信图片_20200927102003.png
然后进行批量,这个有多个域名的ip未输出 微信图片_20200927102101.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-27 17:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表