python访问广州工程技术职业学院官网新闻页面https://www.gzvtc.edu.cn/wechatNews/web/showlist1.aspx?menuOne=10001&menuTwo=10002,并在该页查找,只将新闻中包含“公示”二字的新闻的标题和标题超链、新闻内容存储在excel文件中。
时间: 2023-12-31 09:03:22 浏览: 26
好的,您可以使用 Python 的 requests 和 Beautiful Soup 库来实现这个任务。以下是一个简单的示例代码:
``` python
import requests
from bs4 import BeautifulSoup
import openpyxl
# 发送 GET 请求,获取网页内容
url = "https://www.gzvtc.edu.cn/wechatNews/web/showlist1.aspx?menuOne=10001&menuTwo=10002"
response = requests.get(url)
# 使用 Beautiful Soup 解析网页内容
soup = BeautifulSoup(response.text, "html.parser")
# 找到所有的新闻条目
news_items = soup.find_all("div", class_="info")
# 创建一个 Excel 工作簿
workbook = openpyxl.Workbook()
worksheet = workbook.active
# 在工作表中添加标题行
worksheet.append(["标题", "标题超链", "新闻内容"])
# 遍历每个新闻条目
for item in news_items:
# 找到新闻标题和标题超链
title = item.find("div", class_="title").text.strip()
link = item.find("a")["href"]
full_link = "https://www.gzvtc.edu.cn" + link
# 发送 GET 请求,获取新闻内容
news_response = requests.get(full_link)
news_soup = BeautifulSoup(news_response.text, "html.parser")
content = news_soup.find("div", class_="content").text.strip()
# 如果标题包含“公示”二字,将新闻信息添加到工作表中
if "公示" in title:
worksheet.append([title, full_link, content])
# 保存 Excel 文件
workbook.save("news.xlsx")
```
请注意,这个代码只是一个示例,您需要根据实际情况进行修改和调整。另外,网站的结构可能会随着时间而变化,这可能会导致这个代码无法正常工作。如果您遇到任何问题,请及时调整代码或联系网站管理员。
相关推荐
![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)
![](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)
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)