python爬虫实例

Posted snow-wolf-1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python爬虫实例相关的知识,希望对你有一定的参考价值。

1.九寨沟景点

import asyncio
import requests
from bs4 import BeautifulSoup

base_url = ‘https://www.jiuzhai.com/{0}‘


async def parse_next_html(response):
    soup = BeautifulSoup(response.text,features="html.parser")
    next = soup.select_one(‘.pager .next a‘)
    if next is not None:
        print(base_url.format(next.attrs[‘href‘]),"+++++++")
        response = await get_response(base_url.format(next.attrs[‘href‘]))
        if response is not None:
            await parse_next_html(response)


async def parse_previous_html(response):
    soup = BeautifulSoup(response.text,features="html.parser")
    previous = soup.select_one(‘.pager .previous a‘)
    if previous is not None:
        print(base_url.format(previous.attrs[‘href‘]),"*********")
        response = await get_response(base_url.format(previous.attrs[‘href‘]))
        if response is not None:
            await parse_previous_html(response)


async def get_response(url):
    try:
        return requests.get(url)
    except Exception as e:
        print(e)
        return None


async def run_manager(url):
    response = await get_response(url)
    if response is not None:
        await parse_previous_html(response)

        await parse_next_html(response)


async def main():
    start_url = ‘https://www.jiuzhai.com/news/number-of-tourists/7110-5000-180‘
    await asyncio.gather(
        run_manager(start_url),

    )

if __name__ == ‘__main__‘:

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

2.

以上是关于python爬虫实例的主要内容,如果未能解决你的问题,请参考以下文章

Python 爬虫实例

Python 爬虫实例(10)—— 四行代码实现刷 博客园 阅读数量

python爬虫实例

python爬虫实例

python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例

python爬虫实例详细介绍之爬取大众点评的数据