使用urllib、bs4、re、pymysql来爬取豆瓣250的列表页面信息
时间: 2023-12-03 09:05:18 浏览: 78
好的,这是一个技术问题,我可以回答。您可以使用Python的urllib、bs4、re和pymysql库来爬取豆瓣250的列表页面信息。使用urllib库发送http请求获取页面信息,然后使用bs4库对页面进行解析,使用re库进行数据的提取和清洗,最后使用pymysql库将数据存储到MySQL数据库中。
相关问题
使用urllib、bs4、re、pymysql来爬取豆瓣250的列表页面信息的相关代码
以下是使用urllib、bs4、re、pymysql来爬取豆瓣250的列表页面信息的代码示例:
```python
import urllib.request
from bs4 import BeautifulSoup
import pymysql
# 连接数据库
db = pymysql.connect(host='localhost', user='root', password='password', db='test', charset='utf8')
# 获取页面HTML代码
url = 'https://movie.douban.com/top250'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
req = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
# 使用BeautifulSoup解析HTML代码
soup = BeautifulSoup(html, 'html.parser')
movie_list = soup.find('ol', class_='grid_view').find_all('li')
# 遍历电影列表,获取电影信息并保存到数据库
for movie in movie_list:
# 获取电影排名、电影名称、电影评分、电影链接
rank = movie.find('em').get_text()
title = movie.find('span', class_='title').get_text()
rating = movie.find('span', class_='rating_num').get_text()
link = movie.find('a')['href']
# 使用正则表达式获取电影年份、电影类型等信息
info = movie.find('p', class_='').get_text()
year = re.findall('\d{4}', info)[0]
types = re.findall('\s/\s(.*)', info)[0]
# 插入数据到数据库
cursor = db.cursor()
sql = "INSERT INTO movies(rank, title, rating, link, year, types) VALUES('%s', '%s', '%s', '%s', '%s', '%s')" % (rank, title, rating, link, year, types)
cursor.execute(sql)
db.commit()
# 关闭数据库连接
db.close()
```
希望能对您有所帮助!
使用urllib爬取豆瓣读书页面内容
使用Python的`urllib`库爬取豆瓣读书页面的内容通常涉及以下几个步骤:
1. 导入必要的模块:首先需要导入`urllib.request`模块,它包含了用于打开URL并获取网页内容的函数。
```python
import urllib.request
```
2. 获取网页请求:使用`urllib.request.urlopen()`函数发送HTTP GET请求到豆瓣读书的页面URL。
```python
url = "https://book.douban.com/" # 替换为你想要抓取的具体书页URL
response = urllib.request.urlopen(url)
```
3. 读取网页数据:通过`.read()`方法获取服务器返回的HTML文本。
```python
html_content = response.read().decode('utf-8')
```
4. 解析HTML:由于获取的是原始HTML,接下来通常会使用如BeautifulSoup或lxml等解析库来提取所需的数据,例如书籍标题、作者等信息。
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
title = soup.find('div', class_='bookTitle') # 根据实际页面结构查找元素
title_text = title.get_text()
```
5. 打印或保存结果:最后你可以打印提取的信息,或者将其保存到文件、数据库等地方。
```python
print(title_text)
```
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.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)