python爬取搜狐军事频道(requests,BeautifulSoup)

import requests
import json
from bs4 import BeautifulSoup

headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
    }

list_url_temp="https://v2.sohu.com/public-api/feed?scene=CHANNEL&sceneId=10&page={}&size=20&callback=jQuery112406111535706789408_1600648018249&_=1600648018297"
article_url_temp="https://www.sohu.com/a/"

def getList(url):
    #发送get请求
    resp = requests.get(url,headers=headers)
    jsonStr=resp.text[resp.text.find("["):-2]
    #print(jsonStr)
    jsonObj=json.loads(jsonStr)

    for item in jsonObj:
        print(item.get("title"))
        print(item.get("id"),"---",item.get("authorId"))
        aid=item.get("id")
        authorId=item.get("authorId")
        getArticle(str(aid),str(authorId))


def getArticle(id,authorId):
    article_url=article_url_temp+id+"_"+authorId
    resp = requests.get(article_url,headers=headers)
    # resp.text
    soup=BeautifulSoup(resp.text,'html.parser')
    article=soup.select("article")[0]
    print(article.text)

def main():
    page=0
    for page in range(10):
        print("访问第{}页".format(page))
        url=list_url_temp.format(page)
        getList(url)

main()

原文出处:/show_1EF6NOw8eGg6.html