正则表达式_爬取中国古诗词网与豆瓣热门图书

一.爬取中国诗词网

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

import re
import requests
def handler_url(url):
    header={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36"
    }
    reponse=requests.get(url,header)
    text=reponse.text
    titles=re.findall(r'<div\sclass="cont">.*?<b>(.*?)</b>',text,re.DOTALL)
    years=re.findall(r'<p\sclass="source"><a.*?>(.*?)</a>',text,re.DOTALL)
    autors=re.findall(r'<p\sclass="source">.*?<a.*?>.*?<a.*?>(.*?)</a>',text,re.DOTALL)
    contents_tmp=re.findall(r'<p\sclass="source">.*?<div\sclass="contson"\s.*?>(.*?)</div>',text,re.DOTALL)
    contents=[]
    for c in contents_tmp:
        x=re.sub(r'<.*?>','',c)
        contents.append(x.strip())
    poems=[]
    for value in zip(titles,years,autors,contents):
        title,year,autor,content=value
        poem={
            "title":title,
            "year":year,
            "autor":autor,
            "content":content
        }
        poems.append(poem)
    for a in poems:
        print(a)
def main():
    base_url="https://www.gushiwen.org/default_{}.aspx"
    for i in range(1,7):
        url=base_url.format(i)
        handler_url(url)
if __name__ == '__main__':
    main()

二.爬取豆瓣热门图书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

import re
import requests
HEADERS={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36"
}
def get_detail_page(url):
    resp=requests.get(url,headers=HEADERS)
    text=resp.text
    detail_pages=re.findall(r'<div\sclass="main-bd">.*?<a\shref="(.*?)">.*?</a>',text,re.DOTALL)
    return detail_pages
def handle_detail_page(url):
    book={}
    resp=requests.get(url,headers=HEADERS)
    text=resp.text
    titles=re.findall(r'<div\sclass="article">.*?<span.*?>(.*?)</span>',text,re.DOTALL)
    articles_tmp=re.findall(r'<div\sid="link-report">.*?<p>.*</p>',text,re.DOTALL)
    articles=[]
    for a in articles_tmp:
        x=re.sub(r'<.*?>','',a)
        y=x.replace('\r','').replace('\n','')
        a=re.sub(r'<div\s.*?>','',y)
        b=a.replace(' ','')
        articles.append(b)
    # print(articles)
    # print(titles)
    book={
        'title':titles,
        'article':articles
    }
    print(book)
def main():
    urls=[]
    base_url="https://book.douban.com/review/best/?start={}"
    for i in range(0,41,20):
        url=base_url.format(i)
        urls.append(url)
    for a in urls:
        detail_urls=get_detail_page(a)
        for f in detail_urls:
            handle_detail_page(f)
if __name__ == '__main__':
    main()

三.总结

这两个小的爬虫项目,网站都没有进行反爬处理,所以说对于我们爬出页面信息还是比较简单的,关键在于数据怎么解析,最近一直在看正则表达式,所以找了两个小的项目练一练,这两个小项目也可以使用beatifulsoup或者xml库进行解析,找时间再试试别的方法解析数据吧~

#
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×