这个只爬了一个排行榜其他的忘了写。。。但都一样可以看我写的另一个那个注释比较全,都大同小异 # -*- coding:utf-8 -*- import re import sys import os from time import sleep from bs4 import BeautifulSoup import requests reload(sys) sys.setdefaultencoding('utf-8')
headers={'User-Agent':'自己写'}
def port(url,localPath): req = requests.get(url, headers=headers).text soup = BeautifulSoup(req, 'html.parser') nums = soup.find_all(class_='nums') pp= nums[0].get_text().strip() book_name = soup.find_all(class_='book-name') pp2 = "书名: "+book_name[0].get_text().strip() au_name = soup.find_all(class_='au-name') pp3 ='作者: '+au_name[0].get_text().strip() yp = soup.find_all(class_='fr rank-r') pp4 ='月票排行: '+ yp[0].get_text().replace('\n',"").strip() print pp4 if not os.path.exists(localPath): # 新建文件夹 os.mkdir(localPath) fn = open(localPath+"tt.txt",'a+') ff=pp2+" "+pp3+" "+pp+" "+pp4+"\n" print "正在写入"+ff fn.write(ff) fn.close() def xs(): url='http://book.zongheng.com/rank.html' req=requests.get(url,headers=headers).text soup=BeautifulSoup(req,'html.parser') list=soup.find_all(class_='rank_i_bname fl')//找到class是这个玩意的就是点进去的第2个网页 i=0 for i in range(0,len(list)): url1 = re.findall(r'href="(.*?/book/.*?)"',str(list[i]))#获取href中有book的链接 path='E:/Desktop/img/' uipath = unicode(path, 'utf-8') port(str(url1).strip("['").strip("']"),uipath)
if __name__ == '__main__': xs():