爬取https://www.yanyunxiaoshuo.com/xs/228367/92679328.html网站的文本
时间: 2023-12-02 09:43:21 浏览: 169
以下是爬取https://www.yanyunxiaoshuo.com/xs/228367/92679328.html网站的文本的Python代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.yanyunxiaoshuo.com/xs/228367/92679328.html'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
text = soup.find('div', class_='novelcontent').get_text()
print(text)
```
运行上述代码,即可输出该网页的文本内容。
相关问题
爬取http://jiuye.scetc.edu.cn/reList招聘信息并写入mysql
好的,这个任务可以分为两个部分,第一个是爬取网页内容,第二个是将爬取到的内容写入到 MySQL 数据库中。为了实现这个任务,我们需要使用 Python 语言和一些第三方库。
1. 爬取网页内容
我们可以使用 requests 库来获取网页内容,使用 BeautifulSoup 库来解析 HTML。
代码如下:
```python
import requests
from bs4 import BeautifulSoup
url = 'http://jiuye.scetc.edu.cn/reList'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
```
这样就可以获取到网页内容并将其解析为 BeautifulSoup 对象。
2. 写入 MySQL 数据库
我们可以使用 PyMySQL 库来连接 MySQL 数据库,并使用 SQL 语句将数据写入到数据库中。
代码如下:
```python
import pymysql
# 连接 MySQL 数据库
conn = pymysql.connect(host='localhost', user='root', password='password', database='test', charset='utf8mb4')
cursor = conn.cursor()
# 获取数据
items = soup.find_all('div', {'class': 'col-md-12 col-sm-12 col-xs-12 news_item'})
# 将数据写入到数据库中
for item in items:
title = item.find('a').text
href = item.find('a')['href']
date = item.find('span', {'class': 'date'}).text
sql = f"INSERT INTO jobs(title, href, date) VALUES('{title}', '{href}', '{date}')"
cursor.execute(sql)
conn.commit()
conn.close()
```
这样就可以将爬取到的数据写入到 MySQL 数据库中了。
完整代码如下:
```python
import requests
from bs4 import BeautifulSoup
import pymysql
url = 'http://jiuye.scetc.edu.cn/reList'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 连接 MySQL 数据库
conn = pymysql.connect(host='localhost', user='root', password='password', database='test', charset='utf8mb4')
cursor = conn.cursor()
# 获取数据
items = soup.find_all('div', {'class': 'col-md-12 col-sm-12 col-xs-12 news_item'})
# 将数据写入到数据库中
for item in items:
title = item.find('a').text
href = item.find('a')['href']
date = item.find('span', {'class': 'date'}).text
sql = f"INSERT INTO jobs(title, href, date) VALUES('{title}', '{href}', '{date}')"
cursor.execute(sql)
conn.commit()
conn.close()
```
需要注意的是,我们需要将代码中的数据库连接信息修改为自己的数据库信息。
爬取人民邮电出版社(网址为:https://www.ptpress.com.cn)页面图书价格,生成GET请求并打印出状态码和页面文本信息
好的,我们可以使用 Python 中的 `requests` 和 `BeautifulSoup` 库来实现该功能。先使用 `requests` 库发送 GET 请求,然后使用 `BeautifulSoup` 库解析 HTML 页面,提取出我们需要的数据。
请先在命令行中输入以下命令安装 `requests` 和 `BeautifulSoup` 库:
```
pip install requests beautifulsoup4
```
安装完成后,可以使用以下 Python 代码实现你的需求:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.ptpress.com.cn'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 找到所有图书信息所在的 li 标签
book_list = soup.find_all('li', class_='col-md-2 col-sm-3 col-xs-4')
# 遍历每个图书信息 li 标签
for li in book_list:
# 提取图书名称和价格
name = li.find('h5', class_='title').text.strip()
price = li.find('p', class_='price').text.strip()
# 打印出图书名称和价格
print('名称:', name)
print('价格:', price)
# 打印出状态码和页面文本信息
print('状态码:', response.status_code)
print('页面文本信息:', response.text)
```
在这个代码中,我们先使用 `requests` 库中的 `get()` 方法发送一个 GET 请求,获取 https://www.ptpress.com.cn 的首页页面信息。
然后,我们使用 `BeautifulSoup` 库解析 HTML 页面,并找到所有图书信息所在的 `li` 标签。遍历每个图书信息 `li` 标签,提取图书名称和价格,并打印出来。
最后,我们使用 `print()` 函数打印出状态码和页面文本信息。
注意,本示例代码中的 URL 是一个有效的链接,但并不保证在未来一直有效。如果需要爬取其他网站或页面,请将 `url` 变量替换为相应的链接。
阅读全文
相关推荐














