上海优秀网站建设公司制作公司网页的步骤
- 作者: 五速梦信息网
- 时间: 2026年03月21日 09:22
当前位置: 首页 > news >正文
上海优秀网站建设公司,制作公司网页的步骤,个人性质网站名称,淘宝客网站如何做SEO0.为什么要学习网络爬虫 深度学习一般过程: 收集数据#xff0c;尤其是有标签、高质量的数据是一件昂贵的工作。 爬虫的过程#xff0c;就是模仿浏览器的行为#xff0c;往目标站点发送请求#xff0c;接收服务器的响应数据#xff0c;提取需要的信息#xff0c…0.为什么要学习网络爬虫 深度学习一般过程: 收集数据尤其是有标签、高质量的数据是一件昂贵的工作。 爬虫的过程就是模仿浏览器的行为往目标站点发送请求接收服务器的响应数据提取需要的信息并进行保存的过程。 Python为爬虫的实现提供了工具:requests模块、BeautifulSoup库 1.爬虫练习前言 本次实践使用Python来爬取百度百科中《青春有你2》所有参赛选手的信息。 数据获取https://baike.baidu.com/item/青春有你第二季 普通用户: 打开浏览器 – 往目标站点发送请求 – 接收响应数据 – 渲染到页面上。 爬虫程序: 模拟浏览器 – 往目标站点发送请求 – 接收响应数据 – 提取有用的数据 – 保存到本地/数据库。 本实践中将会使用以下两个模块首先对这两个模块简单了解以下 request模块 requests是python实现的简单易用的HTTP库官网地址http://cn.python-requests.org/zh_CN/latest/ requests.get(url)可以发送一个http get请求返回服务器响应内容。 BeautifulSoup库 BeautifulSoup是一个可以从HTML或XML文件中提取数据的Python库。 网址https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/ BeautifulSoup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml。 BeautifulSoup(markup, “html.parser”)或者BeautifulSoup(markup, “lxml”)推荐使用lxml作为解析器,因为效率更高。 2.程序代码 import json import re import requests import datetime from bs4 import BeautifulSoup from urllib import parse import ostoday datetime.date.today().strftime(%Y%m%d)def crawl_wiki_data():爬取百度百科中《青春有你2》中参赛选手信息返回htmlheaders {#User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36#User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36 Edg/101.0.1210.32User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0}urlhttps://baike.baidu.com/item/青春有你第二季try:response requests.get(url, headersheaders)print(response.status_code)# 将一段文档传入BeautifulSoup的构造方法,就能得到一个文档的对象, 可以传入一段字符串soup BeautifulSoup(response.text, lxml)# 返回的是class为table-view log-set-param的table所有标签tables soup.find_all(table, {class: table-view log-set-param})crawl_table_title 参赛学员for table in tables:# 对当前节点前面的标签和字符串进行查找table_titles table.find_previous(div).find_all(h3)for title in table_titles:if (crawl_table_title in title):return tableexcept Exception as e:print(e)def parse_wiki_data(table_html):从百度百科返回的html中解析得到选手信息以当前日期作为文件名存JSON文件,保存到work目录下bs BeautifulSoup(str(table_html), lxml)all_trs bs.find_all(tr)error_list [\, ]stars []for tr in all_trs[1:]:all_tds tr.find_all(td)star {}# 姓名star[name] all_tds[0].text# 个人百度百科链接star[link] https://baike.baidu.com all_tds[0].find(a).get(href)# 籍贯star[zone] all_tds[1].text# 星座star[constellation] all_tds[2].text# 身高star[height] all_tds[3].text# 体重star[weight] all_tds[4].text# 花语,去除掉花语中的单引号或双引号flower_word all_tds[5].textfor c in flower_word:if c in error_list:flower_word flower_word.replace(c, )# 公司if not all_tds[6].find(a) is None:star[company] all_tds[6].find(a).textelse:star[company] all_tds[6].textstar[flower_word] flower_wordstars.append(star)json_data json.loads(str(stars).replace(\, ))with open(data/ today .json, w, encodingUTF-8) as f:json.dump(json_data, f, ensure_asciiFalse)def crawl_pic_urls():爬取每个选手的百度百科图片并保存with open(data/ today .json, r, encodingUTF-8) as file:json_array json.loads(file.read())statistics_datas []headers {# User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36 Edg/101.0.1210.32}for star in json_array:name star[name]link star[link]# 向选手个人百度百科发送一个http get请求response requests.get(link, headersheaders)# 将一段文档传入BeautifulSoup的构造方法,就能得到一个文档的对象bs BeautifulSoup(response.text, lxml)# 从个人百度百科页面中解析得到一个链接该链接指向选手图片列表页面pic_list_url bs.select(.summary-pic a)[0].get(href)pic_list_url https://baike.baidu.com pic_list_url# 向选手图片列表页面发送http get请求pic_list_response requests.get(pic_list_url, headersheaders)# 对选手图片列表页面进行解析获取所有图片链接bs BeautifulSoup(pic_list_response.text, lxml)pic_list_html bs.select(.pic-list img )pic_urls []for pic_html in pic_list_html:pic_url pic_html.get(src)pic_urls.append(pic_url)# 根据图片链接列表pic_urls, 下载所有图片保存在以name命名的文件夹中down_pic(name, pic_urls)def down_pic(name,pic_urls):根据图片链接列表pic_urls, 下载所有图片保存在以name命名的文件夹中,path work/pics/name/if not os.path.exists(path):os.makedirs(path)for i, pic_url in enumerate(pic_urls):try:pic requests.get(pic_url, timeout15)string str(i 1) .jpgwith open(pathstring, wb) as f:f.write(pic.content)print(成功下载第%s张图片: %s % (str(i 1), str(pic_url)))except Exception as e:print(下载第%s张图片时失败: %s % (str(i 1), str(pic_url)))print(e)continuedef show_pic_path(path):遍历所爬取的每张图片并打印所有图片的绝对路径pic_num 0for (dirpath, dirnames, filenames) in os.walk(path):for filename in filenames:pic_num 1print(第%d张照片%s % (pic_num, os.path.join(dirpath, filename)))print(共爬取《青春有你2》选手的%d照片 % pic_num)if name main:#爬取百度百科中《青春有你2》中参赛选手信息返回htmlhtml crawl_wiki_data()#解析html,得到选手信息保存为json文件parse_wiki_data(html)#从每个选手的百度百科页面上爬取图片,并保存crawl_pic_urls()#打印所爬取的选手图片路径#(/home/aistudio/work/pics/)print(所有信息爬取完成)
- 上一篇: 上海永灿网站建设丽江网页制作公司
- 下一篇: 上海优质网站seo有哪些昆网站在哪里
相关文章
-
上海永灿网站建设丽江网页制作公司
上海永灿网站建设丽江网页制作公司
- 技术栈
- 2026年03月21日
-
上海营销网站设计做设计的搜素材上什么网站
上海营销网站设计做设计的搜素材上什么网站
- 技术栈
- 2026年03月21日
-
上海翼成信息科技有限公司做的什么网站网站服务器放置地查询
上海翼成信息科技有限公司做的什么网站网站服务器放置地查询
- 技术栈
- 2026年03月21日
-
上海优质网站seo有哪些昆网站在哪里
上海优质网站seo有哪些昆网站在哪里
- 技术栈
- 2026年03月21日
-
上海优质网站seo有哪些做兼职网站的项目初衷
上海优质网站seo有哪些做兼职网站的项目初衷
- 技术栈
- 2026年03月21日
-
上海有哪些做网站网页版微信怎么退出
上海有哪些做网站网页版微信怎么退出
- 技术栈
- 2026年03月21日






