开发东莞网站制作公司,网站制作方案包含哪些内容,基于asp的网站开发,自己做网站大概需要多少钱爬虫#xff08;也被称为网络爬虫或网络蜘蛛#xff09;是一种自动化程序#xff0c;它可以在互联网上自动抓取数据。爬虫的基本工作原理通常包括以下几个步骤#xff1a;发送请求#xff1a;爬虫向目标网站发送HTTP请求#xff0c;请求网页内容。接收响应#xff1a;爬… 爬虫也被称为网络爬虫或网络蜘蛛是一种自动化程序它可以在互联网上自动抓取数据。爬虫的基本工作原理通常包括以下几个步骤发送请求爬虫向目标网站发送HTTP请求请求网页内容。接收响应爬虫接收网站返回的响应这通常包括网页的HTML代码、CSS样式、JavaScript脚本等内容。解析内容爬虫使用解析器对接收到的网页内容进行解析提取出需要的数据。存储数据爬虫将提取出的数据存储到本地文件或数据库中以便后续分析和使用。 在Python中有许多强大的库可以帮助你快速开发爬虫如requests、beautifulsoup4、scrapy等。requests库用于发送HTTP请求和接收响应beautifulsoup4库用于解析HTML和XML文档scrapy是一个用于构建爬虫的框架它提供了完整的爬虫开发流程支持。 以下是使用requests库发送HTTP请求并使用BeautifulSoup库解析HTML内容。这个示例将爬取一个网页的标题并打印出来。首先你需要安装requests和beautifulsoup4库。你可以使用pip来安装
bash复制代码 pip install requests beautifulsoup4
然后你可以编写以下Python代码
python复制代码 import requests from bs4 import BeautifulSoup # 目标网页的URL url https://example.com # 发送GET请求 response requests.get(url) # 检查请求是否成功 if response.status_code 200: # 解析网页内容 soup BeautifulSoup(response.text, html.parser) # 查找网页标题通常位于title标签内 title soup.title.string # 打印网页标题 print(f网页标题: {title}) else: print(f请求失败状态码: {response.status_code}) 请将url变量替换为你想爬取的网页的URL。这个示例假设网页的标题位于title标签内这是大多数HTML页面的标准做法。
# -*- codeing utf-8 -*-
from bs4 import BeautifulSoup # 网页解析获取数据
import re # 正则表达式进行文字匹配
import urllib.request, urllib.error # 制定URL获取网页数据
import xlwt # 进行excel操作
#import sqlite3 # 进行SQLite数据库操作findLink re.compile(ra href(.*?)) # 创建正则表达式对象标售规则 影片详情链接的规则
findImgSrc re.compile(rimg.*src(.*?), re.S)
findTitle re.compile(rspan classtitle(.*)/span)
findRating re.compile(rspan classrating_num propertyv:average(.*)/span)
findJudge re.compile(rspan(\d*)人评价/span)
findInq re.compile(rspan classinq(.*)/span)
findBd re.compile(rp class(.*?)/p, re.S)def main():baseurl https://movie.douban.com/top250?start #要爬取的网页链接# 1.爬取网页datalist getData(baseurl)savepath 豆瓣电影Top250.xls #当前目录新建XLS存储进去# dbpath movie.db #当前目录新建数据库存储进去# 3.保存数据saveData(datalist,savepath) #2种存储方式可以只选择一种# saveData2DB(datalist,dbpath)# 爬取网页
def getData(baseurl):datalist [] #用来存储爬取的网页信息for i in range(0, 10): # 调用获取页面信息的函数10次url baseurl str(i * 25)html askURL(url) # 保存获取到的网页源码# 2.逐一解析数据soup BeautifulSoup(html, html.parser)for item in soup.find_all(div, class_item): # 查找符合要求的字符串data [] # 保存一部电影所有信息item str(item)link re.findall(findLink, item)[0] # 通过正则表达式查找data.append(link)imgSrc re.findall(findImgSrc, item)[0]data.append(imgSrc)titles re.findall(findTitle, item)if (len(titles) 2):ctitle titles[0]data.append(ctitle)otitle titles[1].replace(/, ) #消除转义字符data.append(otitle)else:data.append(titles[0])data.append( )rating re.findall(findRating, item)[0]data.append(rating)judgeNum re.findall(findJudge, item)[0]data.append(judgeNum)inq re.findall(findInq, item)if len(inq) ! 0:inq inq[0].replace(。, )data.append(inq)else:data.append( )bd re.findall(findBd, item)[0]bd re.sub(br(\s)?/(\s)?, , bd)bd re.sub(/, , bd)data.append(bd.strip())datalist.append(data)return datalist# 得到指定一个URL的网页内容
def askURL(url):head { # 模拟浏览器头部信息向豆瓣服务器发送消息User-Agent: Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 80.0.3987.122 Safari / 537.36}# 用户代理表示告诉豆瓣服务器我们是什么类型的机器、浏览器本质上是告诉浏览器我们可以接收什么水平的文件内容request urllib.request.Request(url, headershead)html try:response urllib.request.urlopen(request)html response.read().decode(utf-8)except urllib.error.URLError as e:if hasattr(e, code):print(e.code)if hasattr(e, reason):print(e.reason)return html# 保存数据到表格
def saveData(datalist,savepath):print(save.......)book xlwt.Workbook(encodingutf-8,style_compression0) #创建workbook对象sheet book.add_sheet(豆瓣电影Top250, cell_overwrite_okTrue) #创建工作表col (电影详情链接,图片链接,影片中文名,影片外国名,评分,评价数,概况,相关信息)for i in range(0,8):sheet.write(0,i,col[i]) #列名for i in range(0,250):# print(第%d条 %(i1)) #输出语句用来测试data datalist[i]for j in range(0,8):sheet.write(i1,j,data[j]) #数据book.save(savepath) #保存# def saveData2DB(datalist,dbpath):
# init_db(dbpath)
# conn sqlite3.connect(dbpath)
# cur conn.cursor()
# for data in datalist:
# for index in range(len(data)):
# if index 4 or index 5:
# continue
# data[index] data[index]
# sql
# insert into movie250(
# info_link,pic_link,cname,ename,score,rated,instroduction,info)
# values (%s)%,.join(data)
# # print(sql) #输出查询语句用来测试
# cur.execute(sql)
# conn.commit()
# cur.close
# conn.close()# def init_db(dbpath):
# sql
# create table movie250(
# id integer primary key autoincrement,
# info_link text,
# pic_link text,
# cname varchar,
# ename varchar ,
# score numeric,
# rated numeric,
# instroduction text,
# info text
# )
#
#
# #创建数据表
# conn sqlite3.connect(dbpath)
# cursor conn.cursor()
# cursor.execute(sql)
# conn.commit()
# conn.close()# 保存数据到数据库if __name__ __main__: # 当程序执行时# 调用函数main()# init_db(movietest.db)print(爬取完毕)
爬取数据如下