Educoder:爬虫实战——求是网周刊文章爬取

第1关:获取新闻url

import requests
import re

def geturls():
    # ********** Begin ********** #
    url='http://www.qstheory.cn/dukan/qs/2014/2019-01/01/c_1123924172.htm'
    headers={
        'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36'
    }
    response=requests.get(url=url,headers=headers)
    # print(response.encoding)
    response = response.text
    response = response.encode("ISO-8859-1")
    # decode解码,将unicode解码成utf-8
    response = response.decode("utf-8")
    with open('./求是网.html','w',encoding='utf-8') as file:
        file.write(response)
    #     本期重点推荐 
    ex = r'

你可能感兴趣的:(Educoder题目解析,爬虫,r语言,开发语言)