为了实践学习,需要从https://www.example.com网页中提取所有class为listbox的div元素中li里的标题,点击li可进入第二个页面,获取第一个页面的title和第二个页面里class为art_con的div元素中的p中字段,并将数据存入名为sj.txt文件中,应该怎么操作

时间: 2023-06-01 18:01:30 浏览: 47
这个问题涉及到网页爬虫和数据存储两个方面,需要使用Python语言和相关的库来实现。 1. 网页爬虫 使用Python中的requests库发送HTTP请求获取网页内容,使用BeautifulSoup库解析HTML文档,提取需要的数据。 具体的代码可以参考如下: ``` import requests from bs4 import BeautifulSoup url = 'https://www.example.com' response = requests.get(url) html = response.text soup = BeautifulSoup(html, 'html.parser') listboxes = soup.find_all('div', {'class': 'listbox'}) titles = [] for listbox in listboxes: lis = listbox.find_all('li') for li in lis: title = li.get_text() link = li.find('a')['href'] # 点击li进入第二个页面 response2 = requests.get(link) html2 = response2.text soup2 = BeautifulSoup(html2, 'html.parser') # 获取第一个页面的title和第二个页面里class为art_con的div元素中的p中字段 title1 = soup.find('title').get_text() content = soup2.find('div', {'class': 'art_con'}).get_text() # 存入sj.txt文件中 with open('sj.txt', 'a', encoding='utf-8') as f: f.write(title1 + '\n' + content + '\n') ``` 2. 数据存储 将抓取到的数据存储到本地文件中,可以使用Python中的文件操作。 在上面的代码中,使用了with open语句打开文件,文件的写入模式为追加模式('a'),编码格式为UTF-8。每次循环抓取到一个li标签的标题和内容后,就将它们写入文件中。 需要注意的是,如果文件不存在,with open语句会自动创建文件;如果文件已存在,则会在文件末尾追加内容。如果希望每次运行时覆盖原有文件,可以改为写入模式('w')。

相关推荐

import re,tkinter,requests,threading,tqdm as tt root = tkinter.Tk() root.title('在线视频解析') root.geometry('500x590+550+350') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0'} ac = tkinter.Listbox(root, width=50, height=20, font=('黑体', 12)) ac.grid(row=2, columnspan=10, sticky="n" + "s" + "w" + "e") def sousuo(): i = b1.get() ac.delete(0, 'end') def extract_music_info(content): p = '|' content = re.sub(p, '', content, flags=re.S) pattern = re.compile('subject.*?href="(.*?)">(.*?)', flags=re.S) return pattern.findall(content) def search_music(): url = 'https://www.hifini.com/search-' + i + '-1.htm' response = requests.get(url=url, headers=headers) return response.text def update_listbox(music_list): for music in music_list: pppp = music[1] + ":" + music[0] ac.insert('end', pppp) content = search_music() music_list = extract_music_info(content) update_listbox(music_list) def xiazzi(): def download_music(): ppp = ac.get(ac.curselection()) pp = re.search('thread.*?htm', ppp) v = pp.group() url1 = 'https://www.hifini.com/' + v response = requests.get(url=url1, headers=headers) ppp = response.text l2 = re.search('<script>.*?title:..(.*?).,.*?author:.(.*?).,.*?url:..(.*?).,', ppp, flags=re.S) p = 'https://www.hifini.com/' + l2.group(3) response = requests.get(url=p, headers=headers, stream=True) # 设置 stream=True 以启用流式下载 total_size = int(response.headers.get('Content-Length')) music_name = '{}-{}.mp3'.format(l2.group(2), l2.group(1)) progress_bar = tt.tqdm(total=total_size, unit='B', unit_scale=True) # 创建进度条 with open(music_name, 'wb') as f: for data in response.iter_content(chunk_size=1024): progress_bar.update(len(data)) # 更新进度条 f.write(data) progress_bar.close() # 关闭进度条 print(music_name) threading.Thread(target=download_music).start() a1 = tkinter.Label(root, text='音乐下载器', anchor="center", font=('黑体', 24)) a1.grid(row=0, columnspan=10, sticky="n" + "s" + "w" + "e") b1 = tkinter.Entry(root, width=35, font=('黑体', 16), ) b1.grid(row=1, column=3, padx=15) search_button = tkinter.Button(root, text='搜索', command=sousuo) search_button.grid(row=1, column=4) download_button = tkinter.Button(root, text='下载', command=xiazzi) download_button.grid(row=3, column=4) root.mainloop() 将download_button带有下载行为的按钮添加进列表,

最新推荐

recommend-type

C#保存listbox中数据到文本文件的方法

主要介绍了C#保存listbox中数据到文本文件的方法,涉及C#操作listbox数据的相关技巧,需要的朋友可以参考下
recommend-type

C#读取文本文件到listbox组件的方法

主要介绍了C#读取文本文件到listbox组件的方法,涉及C#操作文本文件及listbox组件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

listbox拖拽功能实现

(1)通过鼠标可以将一个ListBox控件中的任意一项拖到另一个ListBox控件中,反之亦然;(2)拖动时,鼠标呈手势状,以拖动操作更具形象化。
recommend-type

C#(WinForm) ComboBox和ListBox添加项及设置默认选择项

主要介绍了C#(WinForm) ComboBox和ListBox添加项及设置默认选择项的的相关资料,需要的朋友可以参考下
recommend-type

WX小程序源码小游戏类

WX小程序源码小游戏类提取方式是百度网盘分享地址
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。