User 用python爬取https://pubmed.ncbi.nlm.nih.gov/37087179/网页的作者,题名,出版时间,来源,卷,期,页,PMID信息,将这些信息以txt文件的形式保存到本地log文件夹里面
时间: 2024-06-08 07:07:42 浏览: 15
好的,你可以使用 Python 的 requests 库和 BeautifulSoup 库来实现这个功能。具体步骤如下:
1. 导入所需的库:
```python
import requests
from bs4 import BeautifulSoup
```
2. 发送 HTTP 请求,获取网页内容:
```python
url = 'https://pubmed.ncbi.nlm.nih.gov/37087179/'
response = requests.get(url)
html = response.text
```
3. 使用 BeautifulSoup 解析网页内容,并找到需要的信息:
```python
soup = BeautifulSoup(html, 'html.parser')
title = soup.find('h1', {'class': 'heading-title'}).text.strip()
authors = ', '.join([a.text.strip() for a in soup.find_all('span', {'class': 'authors-list-item'})])
date = soup.find('span', {'class': 'cit'}).text.strip()
journal = soup.find('a', {'class': 'journal-title-link'}).text.strip()
volume = soup.find('span', {'class': 'cit'}).find_all('a')[1].text.strip()
issue = soup.find('span', {'class': 'cit'}).find_all('a')[2].text.strip()
pages = soup.find('span', {'class': 'pg'}).text.strip()
pmid = soup.find('div', {'class': 'pubmed-pmid'}).text.strip().split(':')[1].strip()
```
4. 将获取到的信息保存到文本文件中:
```python
with open('log/pubmed.txt', 'w', encoding='utf-8') as f:
f.write(f'Title: {title}\n')
f.write(f'Authors: {authors}\n')
f.write(f'Date: {date}\n')
f.write(f'Journal: {journal}\n')
f.write(f'Volume: {volume}\n')
f.write(f'Issue: {issue}\n')
f.write(f'Pages: {pages}\n')
f.write(f'PMID: {pmid}\n')
```
最后,你可以将上述代码整合到一个完整的程序中,如下所示:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://pubmed.ncbi.nlm.nih.gov/37087179/'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
title = soup.find('h1', {'class': 'heading-title'}).text.strip()
authors = ', '.join([a.text.strip() for a in soup.find_all('span', {'class': 'authors-list-item'})])
date = soup.find('span', {'class': 'cit'}).text.strip()
journal = soup.find('a', {'class': 'journal-title-link'}).text.strip()
volume = soup.find('span', {'class': 'cit'}).find_all('a')[1].text.strip()
issue = soup.find('span', {'class': 'cit'}).find_all('a')[2].text.strip()
pages = soup.find('span', {'class': 'pg'}).text.strip()
pmid = soup.find('div', {'class': 'pubmed-pmid'}).text.strip().split(':')[1].strip()
with open('log/pubmed.txt', 'w', encoding='utf-8') as f:
f.write(f'Title: {title}\n')
f.write(f'Authors: {authors}\n')
f.write(f'Date: {date}\n')
f.write(f'Journal: {journal}\n')
f.write(f'Volume: {volume}\n')
f.write(f'Issue: {issue}\n')
f.write(f'Pages: {pages}\n')
f.write(f'PMID: {pmid}\n')
```
运行以上代码后,你会在 log 目录下看到一个名为 pubmed.txt 的文件,里面包含了所需的信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)