阅读背景:

【Python爬虫案例学习】分析Ajax请求并抓取今日头条街拍图片

来源:互联网 

1.抓取索引页内容
应用requests要求目的站点,得到索引网页HTML代码,返回成果。

from urllib.parse import urlencode
from requests.exceptions import RequestException
import requests
"""
遇到不懂的问题?Python学习交换群:821460695满足你的需求,资料都已上传群文件,可以自行下载!
"""
def get_page_index(offset, keyword):
    headers = { "User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36" }
    data = {
        "format": "json",
        "offset": offset,
        "keyword": keyword,
        "autoload": "true",
        "count": 20,
        "cur_tab": 1,
        "from": "search_tab",
        "pd": "synthesis",
    }
    url = "https://www.toutiao.com/search_content/?" + urlencode(data)
    response = requests.get(url, headers=headers);
    try:
        if response.status_code == 200:
            return response.text
        return None
    except RequestException: 
        print("要求索引页失败")
        return None

def main():
    html = get_page_index(0,"街拍")
    print(html)

if __name__=="__main__":
    main()from



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: