爬取豆瓣图书首页的图书信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40567229/article/details/84201041

使用requests库和re库来爬取豆瓣图书首页的图书信息 

import requests
import re

content = requests.get("http://book.douban.com").text    #get函数获取豆瓣图书网页代码
pattern = re.compile('<li.*?cover.*?href="(.*?)".*?alt="(.*?)".*?author">(.*?)<',re.S)    #complip函数保存正则式
result = re.findall(pattern,content)    #findall寻找符合正则式的信息
for results in result:
    url,name,author = results
    url = re.sub('\s',' ',url)    #将换行符转换为空格
    name = re.sub('\s',' ',name)
    author = re.sub('\s','',author)
    print(url,name,author)

爬取结果: 

猜你喜欢

转载自blog.csdn.net/weixin_40567229/article/details/84201041