在import requests,time
from bs4 import BeautifulSoup
file = open('go007.txt','w',encoding='utf-8')
header = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding':'gzip,deflate',
'Accept-Language':'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6',
'Connection':'keep-alive',
'Host':'www.go007.com',
'Referer':'http://www.go007.com/ditu/peixunjigou_shijiazhuang_wudaopeixun/',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0(WindowsNT6.1;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/72.0.3626.109Safari/537.36'
}
def go007(url):
for num in range(1,30):
url_get = url + str(num)
try:
res = requests.get(url=url_get,headers=header)
soup = BeautifulSoup(res.text,'lxml')
tags = soup.find('ul',attrs={"class":"school-col-box"})
names1 = tags.find_all(['h3','span']) #h3有机构名 span同时取地址 不知如何只取电话 只好同时取再清洗
#names2 = tags.find_all('span')
for i in names1:
temp = i.text
print(temp)
file.write(temp +'\n')
file.flush()
except:
pass
time.sleep(10)
if __name__ == '__main__':
url1 = 'http://www.go007.com/ditu/peixunjigou_tangshan_yishupeixun/p' #艺术培训网址
url2 = 'http://www.go007.com/tangshan/wudaopeixun/p' #舞蹈培训网址
go007(url1)
go007(url2)
城市分际际爬虫
最新推荐文章于 2021-04-24 17:51:42 发布