爬虫获取某神社的神秘代码"/>
使用网络爬虫获取某神社的神秘代码
Python随笔1(使用网络爬虫获取某神社的神秘代码)
from decimal import getcontext
import requests
import socket
import http.client
import re
import openpyxl
import numpy as np
#import urllib.requests
from bs4 import BeautifulSoupdef get_connect(url):res = requests.get(url)res.encoding = 'utf-8'return res.textif __name__ == '__main__':#定义3个列表用来存放标题,简介,神秘代码的内容title_list = []connect_list = []xunlei_list = []#循环抓取页面1-3的所有内容的神秘代码for page in range(1,3):url='https://‘具体的怕被和谐就不发了’'+str(page)+'/'html = get_connect(url)#获取html,接着进入bs4操作bs = BeautifulSoup(html,"html.parser")body = bs.bodydata = body.find_all('a',{'class':'more-link'})#遍历网页上的class为more-link的a标签for i in range(0,len(data)):url_next = data[i]['href']#进入class为more-link的标签的href的内容的网站中,也就是详细页面html_next = get_connect(url_next)bs_next = BeautifulSoup(html_next,"html.parser")body_next = bs_next.bodydata_next = body_next.find_all('p')data_title_next = body_next.find('h1',{'class':'entry-title'})#读取第一个p和title放入列表title_list.append(data_title_next.get_text())connect_list.append(data_next[0].get_text())xl = ''for j in range(0,len(data_next)):pd = data_next[j].get_text()obj = re.search(r'^[A-Za-z0-9_ ]+$',pd, flags=0)#抓取符合神秘代码正则表达式的内容if(obj):x = obj.group().replace(' ','')if(len(x)==40):#神秘代码长度为40,去掉空格以后长度还是40的默认为神秘代码xl += xxunlei_list.append(xl)#将神秘代码添加到列表中#将3个列表打印在xlsx文件中,这里是直接创建一个新的1.xlsx文件。wb = openpyxl.Workbook()wb.create_sheet(title='全彩漫画')sheet = wb['全彩漫画']for i in range(0,len(title_list)):sheet['A'+ str(i+1)] = title_list[i]sheet['B'+ str(i+1)] = connect_list[i]sheet['C'+ str(i+1)] = xunlei_list[i]wb.save('1.xlsx')
总结
python网络爬虫的一个小实例,第一次写,也是第一次用CSDN,勿喷。
更多推荐
使用网络爬虫获取某神社的神秘代码
发布评论