访问广州工程技术职业学院官网新闻页面https://www.gzvtc.edu.cn/wechatNews/web/showlist1.aspx?menuOne=10001&menuTwo=10002,并在该页查找,只将新闻中包含“公示”二字的新闻的标题和标题超链、新闻内容存储在excel文件中。
时间: 2023-12-31 08:03:15 浏览: 37
你可以使用 Python 的第三方库 `requests` 和 `BeautifulSoup` 来访问网页、解析 HTML,并使用 `openpyxl` 来写入 Excel 文件。下面是一个实现该功能的示例代码:
```python
import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
# 访问网页并解析 HTML
url = "https://www.gzvtc.edu.cn/wechatNews/web/showlist1.aspx?menuOne=10001&menuTwo=10002"
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
# 创建 Excel 文件并写入表头
wb = Workbook()
ws = wb.active
ws.append(["标题", "标题超链", "新闻内容"])
# 遍历所有新闻标题和链接,查找包含“公示”二字的新闻并写入 Excel 文件
news_list = soup.find_all("a", class_="news_list_title")
for news in news_list:
title = news.get_text()
link = news.get("href")
news_response = requests.get(link)
news_soup = BeautifulSoup(news_response.content, "html.parser")
content = news_soup.find("div", class_="news_content").get_text()
if "公示" in title:
ws.append([title, link, content])
# 保存 Excel 文件
wb.save("news.xlsx")
```
这将在当前工作目录下创建一个名为 `news.xlsx` 的 Excel 文件,其中包含所有新闻标题和内容中包含“公示”二字的新闻。你可以根据需要对代码进行修改,以获取其他信息或使用其他条件过滤新闻。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
import requests import os import time import json from tqdm import tqdm import re def taopiaopiao(): headers = { 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36 Edg/113.0.1774.57' } time.sleep(0.5) url = "https://dianying.taobao.com/showList.htm?spm=a1z21.6646273.city.2.4ed46d6ekOc3wH&n_s=new&city=310100" response = requests.get(url, headers=headers) html = response.text print("网页信息已获取…") time.sleep(0.5) destinationPath = "result.txt" fd = open(destinationPath, "w+", encoding='utf-8') fd.writelines(html) end = html.find('') if end != -1: html = html[:end] #print(html) fd.close() s = '<img width="160" height="224" data-src="(.*?)" src=' + \ '.*?(.+?).*?(\d.\d)?' + \ ".*?导演:(.*?)" + ".*?主演:(.*?)" + ".*?类型:(.*?)" + \ ".*?地区:(.*?)" + ".*?语言:(.*?)" + ".*?片长:(.*?)" + \ ".*?" pattern = re.compile(s, re.S) items = re.findall(pattern, html) #print(items) destinationPath = "items.json" fd = open(destinationPath, "w+", encoding='utf-8') json.dump(items, fd) fd.close() dir_name = "./images" if not os.path.exists(dir_name): os.mkdir(dir_name) cnt = 0 for item in tqdm(items): url = item[0] file_name = str(cnt) + ".jpg" cnt += 1 response = requests.get(url, headers=headers) with open(dir_name + "/" + file_name, 'wb') as f: f.write(response.content) info = "图片文件: {0:25}{1}".format(file_name, " 成功下载...") print(info) return items if __name__ == "__main__": taopiaopiao()
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)