爬虫12-太平洋汽车

import requests
from bs4 import BeautifulSoup


def check(items):
    if len(items) == 0:
        return "No Public House"
    else:
        return items


def got_html(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) '
                             'Chrome/69.0.3497.100 Safari/537.36'}
    response = requests.get(url, headers=headers)
    html = response.content
    return html


def parse_html(html):
    html = BeautifulSoup(html, 'lxml')
    results = html.select('#topic_list > form > table > tbody')
    results.pop(0)
    item_list = []
    for result in results:
        try:
            name = result.select('tr > th > span.checkbox_title > a')[0].get_text().strip()
            author = result.select('tr > td.author > cite > a')[0].get_text().strip()
            date = result.select('tr > td.author > em')[0].get_text().strip()
            callback = result.select('tr > td.nums > cite')[0].get_text().strip()
            look_times = result.select('tr > td.nums > em')[0].get_text().strip()
            last_public = result.select('tr > td.lastpost > cite > a')[0].get_text().strip()
            date_time = result.select('tr > td.lastpost > em')[0].get_text().strip()
            item_list.append(name)
            item_list.append(author)
            item_list.append(date)
            item_list.append(callback)
            item_list.append(look_times)
            item_list.append(last_public)
            item_list.append(date_time)
        except:
            pass
    return item_list


if __name__ == '__main__':
    strat_url = 'https://bbs.pcauto.com.cn/forum-17442-'
    end_url = '.html'
    for page in range(1, int(input('终止页码'))+1):
        url = strat_url + str(page) + end_url
        html = got_html(url)
        parse_html(html)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值