Python爬取海安網所有號碼

2021-07-29 12:38:05 字數 1561 閱讀 8933

注:1.本程式採用ms sql server資料庫,請執行前手動修改程式開始處的資料庫連線資訊。

2.需要pyodbc庫的支援

import requests,re,sys

from bs4 import beautifulsoup

import pyodbc

pages = set()

conn = pyodbc.connect(r'driver=;server=wxs-pc;database=test;uid=sa')

cursor = conn.cursor()

def getlinks(pageurl):

global pages

r = requests.get(pageurl , timeout =30)

demo = r.text

bsobj = beautifulsoup(demo,'html.parser')

#去除外鏈

for link in bsobj.findall('a',href=re.compile("(www.haianw.com)+")):

if 'href' in link.attrs:

if link.attrs['href'] not in pages:

newpage = link.attrs['href']

findphone(newpage)

pages.add(newpage)

getlinks(newpage)

#將當前url中的手機號存入字典

def findphone(url):

numbers = {}

r = requests.get(url,timeout=30)

data = r.text

#**號碼和手機號碼正規表示式

phone_list = re.findall(r"\d-\d|\d-\d|1[34578]\d",data)

phone_list = list(set(phone_list))

for phone in phone_list:

numbers[phone] = url

writephone(numbers)

def writephone(numbers):

global cursor

global conn

for k,v in numbers.items():

temp = "insert into numbers (link,number) values ('{}','{}')".format(v,k)

cursor.execute(temp)

conn.commit()

if __name__ == '__main__':

#設定遞迴深度為一百萬,防止爬蟲崩潰

sys.setrecursionlimit(1000000)

print("開始爬取全站鏈結...")

try:

getlinks('')

except exception:

print('爬蟲發生崩潰錯誤,已停止爬取...')

python爬蟲爬取車標網所有車標Logo

1.依賴 requests beautifulsoup lxml2.爬取 import requests from bs4 import beautifulsoup list a b c d f g h j k l m n o p q r s t w x y z 用於存車標網分類位址 newlist...

python爬取HDU所有題目

還沒有爬具體題目資訊,待更新。coding utf 8 爬取hdu題目儲存到本地excel import requests from bs4 import beautifulsoup import xlsxwriter 儲存題目資訊二維陣列 allproblem 建立excel檔案 hduprobl...

Python爬取散文網散文

配置python 2.7 bs4 requests 安裝 用pip進行安裝 sudo pip install bs4 sudo pip install requests 簡要說明一下bs4的使用因為是爬取網頁 所以就介紹find 跟find all find跟find all的不同在於返回的東西不同...