page_text = "\n".join(page_text_list) 是什么意思
时间: 2023-06-16 19:07:28 浏览: 38
这行代码是将一个列表 `page_text_list` 中的所有元素按照换行符 `\n` 连接起来,形成一个字符串 `page_text`。具体来说,`join()` 方法会将列表中的字符串元素按照指定的分隔符连接起来,返回一个新的字符串。在这里,我们使用换行符 `\n` 作为分隔符,将列表中的所有字符串元素连接成一个多行文本字符串。
相关问题
修改这段代码并写出来import requests from bs4 import BeautifulSoup # 爬取的页面数量 num_pages = 5 # 打开每个页面并获取 HTML 内容 for page_num in range(1, num_pages + 1): url = f"https://www.bernama.com/bm/index.php/archive.php?page={page_num}" response = requests.get(url) html_content = response.text # 解析 HTML 内容,获取新闻标题和内容 soup = BeautifulSoup(html_content, "html.parser") news_list = soup.find_all("div", class_="row news-row") for news in news_list: title = news.find("a").text.strip() link = news.find("a")["href"] news_response = requests.get(link) news_html = news_response.text news_soup = BeautifulSoup(news_html, "html.parser") content_list = news_soup.find_all("div", class_="col-md-12 news-body") content = "\n".join([p.text for p in content_list[0].find_all("p")]) print(content) # 将新闻标题和内容保存到 txt 文件 with open(f"page{page_num}_{title}.txt", "w") as f: f.write(f"Title: {title}\n") f.write(f"Link: {link}\n") f.write(f"Content:\n{content}\n")
import requests
from bs4 import BeautifulSoup
# 爬取的页面数量
num_pages = 5
# 打开每个页面并获取 HTML 内容
for page_num in range(1, num_pages + 1):
url = f"https://www.bernama.com/bm/index.php/archive.php?page={page_num}"
response = requests.get(url)
html_content = response.text
# 解析 HTML 内容,获取新闻标题和内容
soup = BeautifulSoup(html_content, "html.parser")
news_list = soup.find_all("div", class_="row news-row")
for news in news_list:
title = news.find("a").text.strip()
link = news.find("a")["href"]
# 打开每个新闻链接并获取 HTML 内容
news_response = requests.get(link)
news_html = news_response.text
# 解析新闻 HTML 内容,获取新闻内容
news_soup = BeautifulSoup(news_html, "html.parser")
content_list = news_soup.find_all("div", class_="col-md-12 news-body")
content = "\n".join([p.text for p in content_list[0].find_all("p")])
# 将新闻标题和内容保存到 txt 文件
with open(f"page{page_num}_{title}.txt", "w") as f:
f.write(f"Title: {title}\n")
f.write(f"Link: {link}\n")
f.write(f"Content:\n{content}\n")
# 文件保存路径 file_path = './test' create_file(file_path) # 获取图片 for i in img_list: try: img_url = i.xpath('./@src')[0] except: img_url = i.xpath('./@data-src')[0] # 文件名称 file_name = f'{file_path}/page_{cnt}.jpg' print(file_name, img_url) # 下载保存图片 resp = requests.get(img_url) with open(file_name, 'wb') as f: f.write(resp.content) cnt += 1修改这部分代码,使得存储文件名和路径分开来
要将存储文件名和路径分开来,可以在 `file_path` 和 `file_name` 中分别使用不同的变量来构造。具体来说,可以定义一个变量 `file_dir` 来存储文件夹路径,再根据当前图片序号 `cnt` 构造出文件名 `file_name`,然后将 `file_dir` 和 `file_name` 合并起来得到完整的文件路径。
修改后的代码如下:
```python
# 文件保存路径
file_dir = './test'
create_file(file_dir)
# 获取图片
cnt = 0
for i in img_list:
try:
img_url = i.xpath('./@src')[0]
except:
img_url = i.xpath('./@data-src')[0]
# 文件名称
file_name = f'page_{cnt}.jpg'
print(file_name, img_url)
# 下载保存图片
resp = requests.get(img_url)
file_path = os.path.join(file_dir, file_name) # 合并文件路径
with open(file_path, 'wb') as f:
f.write(resp.content)
cnt += 1
```
这样,存储文件名和路径就被分开来了,文件名和路径的构造也更加清晰易懂。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)