目前分類:程式進修 (7)

瀏覽方式: 標題列表 簡短摘要

{基本構造}

<!DOCTYPE html>

她家 發表在 痞客邦 留言(0) 人氣()

import requests
import time
import json
from bs4 import BeautifulSoup

PTT_url = 'https://www.ptt.cc'

def get_web_page(url):
    resp = requests.get(
        url = url,
        cookies = {'over18':'1'}
    )
    if resp.status_code !=200:
        print('Ivalid url:', resp.url)
        return None
    else:
        return resp.text

def get_articles(dom, date):
    soup = BeautifulSoup(dom, 'html.parser')
    paging_div = soup.find('div', 'btn-group btn-group-paging')
    prev_url = paging_div.find_all('a')[1]['href']
    articles = []
    divs = soup.find_all('div', 'r-ent')
    for d in divs:
        if d.find('div', 'date').text.strip() == date:
            push_count = 0
            push_str = d.find('div', 'nrec').text
            if push_str:
                try:
                    push_count = int(push_str)
                except ValueError:
                    if push_str == '':
                        push_count = 99
                    elif push_str.startswith('X'):
                        push_count = -10
            if d.find('a'):
                href = d.find('a')['href']
                title = d.find('a').text
                author = d.find('div', 'author').text if d.find('div', 'author') else ''  # author = d.find('div', 'author').text if d.find('div', 'author') else ''
                articles.append({
                    'title': title,
                    'href': href,
                    'push_count': push_count,
                    'author': author
                })
        return articles, prev_url

def get_author_ids(posts, pattern):
    ids = set()
    for post in posts:
        if pattern in post['author']:
            ids.add(post['author'])
    return ids

if __name__ == '__main__':
    current_page = get_web_page(PTT_url + '/bbs/Gossiping/index.html')
    if current_page:
        articles = []
        today = time.strftime("%m/%d").lstrip('0')
        current_articles, prev_url = get_articles(current_page, today)
        while current_articles:
            articles += current_articles
            current_page = get_web_page(PTT_url + prev_url)
            current_articles, prev_url = get_articles(current_page, today)
        print(get_author_ids(articles, '5566'))
        print('今天有', len(articles), '篇文章')
        threshold = 50
        print('熱門文章(> %d ):' % (threshold))
        for a in articles:
            if int(a['push_count']) > threshold:
                print(a)
        with open('gossiping.json', 'w', encoding='utf-8') as f:
            json.dump(articles, f, indent=2, sort_keys=True, ensure_ascii=False)

她家 發表在 痞客邦 留言(0) 人氣()

import requests
import re
from bs4 import BeautifulSoup


def main():
    URL = 'https://www.dcard.tw/f'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    resp = requests.get(URL, headers=headers)
    soup = BeautifulSoup(resp.text, 'html.parser')

    articles = []
    # 找出所有 role="article"  <article> tag
    article = soup.find_all('article', {"role": "article"})
    for div in article:
        author = div.find("div",{'class': 'euk31c-2 kFrzdN'}).text.strip()
        title = div.find("h2").text.strip()
        href = div.h2.a['href']
        text = div.find("div", class_="uj732l-0 eLjDMI").text.strip()

        articles.append({
            'author':author,
            'title': title,
            'href' : href, 
            'text' : text
        })
    print(articles, (len(articles)))


if __name__ == '__main__':
    main()


文章標籤

她家 發表在 痞客邦 留言(0) 人氣()

{買賣物品折扣問題}

[主程式]

她家 發表在 痞客邦 留言(0) 人氣()

{寶可夢物件使用藍圖}

[主程式]-[使用物件]

她家 發表在 痞客邦 留言(0) 人氣()

用method找質數

public class hello

她家 發表在 痞客邦 留言(0) 人氣()

"只有不小心全軍覆沒,沒有不小心大獲全勝,每一個成功都必須要很努力很努力"

陣列(一):

她家 發表在 痞客邦 留言(0) 人氣()