如何通过使用BeautifulSoup获取IP地址并输出到CSV?

2024-05-14 19:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

import requests    
from bs4 import BeautifulSoup

url ='https://myip.ms/browse/blacklist/Blacklist_IP_Blacklist_IP_Addresses_Live_Database_Real-time'

response = requests.get(url)

data = response.text

soup = BeautifulSoup(data, 'html.parser')

ipList = soup.find("td",{"class": "row_name"})

rows = ipList.findAll('td')
for tr in rows:
  cols = td.findAll('td')
  if len(cols) > 0:
     print (ip.cols.text.strip())

我正在使用BeautifulSoup进行网页抓取,遇到了一些问题。请问为什么我不能从数据库表中获取IP地址。如何将结果输出到CSV文件?在


Tags: textimportipurldataresponserequestsrows
1条回答
网友
1楼 · 发布于 2024-05-14 19:24:11

问题是您将find()用于ipList,它只获取一个ip,您可以使用findall()或{}来返回ip数组。在

import requests
from bs4 import BeautifulSoup
url ='https://myip.ms/browse/blacklist/Blacklist_IP_Blacklist_IP_Addresses_Live_Database_Real-time'
response = requests.get(url).content
soup = BeautifulSoup(response, 'html.parser')
ipList = soup.select(".row_name")
with open('ip_output.csv', 'w') as f:
    for ips in ipList:
        f.write(ips.find('a').text + '\n')

输出csv

^{pr2}$

相关问题 更多 >

    热门问题