Python 如何使用BeautifulSoup获取IP地址并输出到CSV?
我正在使用BeautifulSoup进行网页抓取,我遇到了一些问题。我可以知道为什么我无法从数据库表中获取IP地址吗。如何将结果输出到CSV文件?问题是您正在使用Python 如何使用BeautifulSoup获取IP地址并输出到CSV?,python,web-scraping,beautifulsoup,export-to-csv,Python,Web Scraping,Beautifulsoup,Export To Csv,我正在使用BeautifulSoup进行网页抓取,我遇到了一些问题。我可以知道为什么我无法从数据库表中获取IP地址吗。如何将结果输出到CSV文件?问题是您正在使用find()用于ipList,它只获取一个ip,您可以使用findall()或select返回ip数组 import requests from bs4 import BeautifulSoup url ='https://myip.ms/browse/blacklist/Blacklist_IP_Blacklist_IP_A
find()
用于ipList,它只获取一个ip,您可以使用findall()
或select
返回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)
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())
以csv格式输出
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')
非常感谢。但是我可以知道proxies=proxies是做什么的吗?我的错,忽略proxies参数,我只是用它来传递防火墙谢谢你帮我解决了我的问题!非常感谢!
195.154.251.86
37.140.192.194
80.94.174.55
175.103.39.28
90.173.129.250
51.15.146.121
...
...