如何用python获取http://paper.people.com.cn/rmrb/html/2023-04/21/nw.D110000renmrb_20230421_5-01.htm的文本内容
时间: 2023-10-14 22:05:45 浏览: 50
您可以使用Python中的requests和BeautifulSoup库来获取网页上的文本内容。下面是一个示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://paper.people.com.cn/rmrb/html/2023-04/21/nw.D110000renmrb_20230421_5-01.htm'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
text_list = []
for p in soup.find_all('p'):
text_list.append(p.get_text())
text = '\n'.join(text_list)
print(text)
```
这段代码会访问给定的URL,并使用BeautifulSoup从HTML页面中提取文本。最后,通过将所有段落的文本连接成一个字符串,我们得到了页面的文本内容。
相关问题
帮我用Python代码写一个可以批量下载人民日报的PDF文件并且统计其中的词语重复率的语句代码
以下是可以批量下载人民日报的PDF文件并统计其中词语重复率的Python代码:
```
import requests
from bs4 import BeautifulSoup
import os
import PyPDF2
import re
# 获取人民日报的PDF下载链接
def get_pdf_links():
url = 'http://paper.people.com.cn/rmrb/html/{}/nbs.D110000renmrb_01.htm'
pdf_links = []
for i in range(1, 6):
date = '2022-01/0{}'.format(str(i))
res = requests.get(url.format(date))
soup = BeautifulSoup(res.text, 'html.parser')
links = soup.select('div.right_title-list ul li a')
for link in links:
if link.text.endswith('版PDF'):
pdf_links.append(link['href'])
return pdf_links
# 下载PDF文件
def download_pdf(url, path):
res = requests.get(url)
with open(path, 'wb') as f:
f.write(res.content)
# 统计词语重复率
def count_word_frequency(pdf_path, word):
with open(pdf_path, 'rb') as f:
pdf_reader = PyPDF2.PdfFileReader(f)
text = ''
for i in range(pdf_reader.getNumPages()):
text += pdf_reader.getPage(i).extractText()
text = re.sub(r'\s+', ' ', text)
total_words = len(text.split())
word_count = text.count(word)
frequency = word_count / total_words
return frequency
if __name__ == '__main__':
pdf_links = get_pdf_links()
for link in pdf_links:
filename = link.split('/')[-1]
path = os.path.join('pdfs', filename)
download_pdf(link, path)
frequency = count_word_frequency(path, '中国')
print('{} 中词语重复率:{}'.format(filename, frequency))
```
请注意,这段代码仅供参考,可能需要根据实际情况进行修改和调整。
yolov5加快训练速度
将正样本匹配过程加入r,加快了运算速度;加入EMA效果变好。此外,yv5还支持不同尺寸模型训练,分别为s、m、、x版本的yv5,支持step、s学习率下降法、支持m、sg优化器选择、支持学习率...。如果使用GPU训练,可以参考这个网页进行训练:https//github.com/urytics/yv5/issues/475,而不是主页上的,主页上的会仅仅使用CPU训练。当使用单机多卡时,一定要使用最新的DistribuDPar而不要再使用老旧的DPar方式。如果加上训练poch,可以使用如下命令:pyth -m torch.istribu.launch --pr_per_ 4 trai.py --pochs 100 --batch 128 -- .//rb_rmrb.yam --weights ./yv5.p --vi ,1,2,3。\n\