python3抓取代理服务器ip

在使用python3爬取网页,解析网页,然后结果入文件,入数据库

解析网页用到了BeautifulSoup,入库用到了pymsql

当然了这两个都是第三方的库,需要安装

具体代码如下:

#!/usr/bin/python
import urllib.request
import pymysql
from bs4 import BeautifulSoup
url="http://proxy.com.ru"
soup=BeautifulSoup(urllib.request.urlopen(url),from_encoding='utf-8')
#print(soup)
tables=soup.findAll('table')
i=0
j=0
for table in tables:
    if i==7:
        print('开始抓取解析ip')
        values=[]
        f=open("ip.txt","w")
        #print(table)
        trs=table.findAll('tr')
        for tr in trs:
           if j>0:
               tds=tr.findAll('td')
               f.write(tds[1].text+":"+tds[2].text+"\n")
               values.append(tds[1].text+":"+tds[2].text)
           j=j+1
        f.close()
        #数据库操作
        try:
            conn=pymysql.connect(host='localhost',user='root',passwd='1234',db='test',charset='utf8')
            cur=conn.cursor()
            sql='delete from proxy_ip;insert into proxy_ip (ip) values'
            dbparam=''
            for param in values:
                dbparam+="('"+param+"'),"
            sql+=dbparam[:-1]
            print(sql)
            cur.execute(sql)
            conn.commit()
            cur.close()
            conn.close()
        except pymysql.Error as e:
            print("pyMysql Error {0}".format(e))
        break
    i=i+1

print("完成")


发布了97 篇原创文章 · 获赞 15 · 访问量 65万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章