http协议-数据请求

news/2024/6/16 21:01:52 标签: 爬虫, python, 多线程

编写程序实现新闻网页数据的请求和获取数据的保存: 进入一个学校的新闻首页,分析各不同新闻网页url之间的区别和联系,并根据得到的规律通过程序生成所要请求的网页的url(前30个页面)。 使用requests库通过http协议的get方法向web server 请求30个页面的数据,要求:(1)30个请求分别由5个线程实现,每个线程负责6个url页面的请求;(2)每个线程中的不同页面请求的时间间隔是3秒;(3)当线程中成功获取某一个新闻网页数据后,需要在屏幕上输出相应的状态信息(如。。。网页请求成功) 对于每个获取的网页数据,分别将其写入到本地相应的html文件中,要求本地网页数据文件的文件名为pageXXX(XXX为请求页面的编号) 对生成的文件进行验证,如果出现中文乱码的问题,请对可能的原因进行分析,并给出可行的解决方案。

import requests
import threading
import time
header = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"
}
url_list =[]
for i in range(30):
    count = 41 - i
    count_str = str(count)
    url_list.append("http://www.ldu.edu.cn/index/zhyw/"+ count_str +".htm")
    # url_list[i] = "http://www.ldu.edu.cn/index/zhyw/"+ count_str +".htm"

def MyThread(count_thread):
        # count_thread = 4
        print(count_thread)
        for i in range(6):
            time.sleep(3)
            url = url_list[i+count_thread*6]
            # print(url)
            response = requests.get(url,params = None,headers = header)
            print(response.status_code)  # 获取状态码
            print(response.cookies)  # 获取cookie,这种获取的cookie不能用于登录,因为它不是完整的
            print(response.headers)  # 获取响应头
            data = response.text
            stringtext = str(i + count_thread * 6)
            encoding_str = response.encoding
            f = open(r'baidu' + stringtext + '.html', 'w', encoding=encoding_str)
            f.write(data)
            f.close()
            # print(data)

def main():
    for i in range(5):
        t = threading.Thread(target=MyThread, args=(i,))
        t.start()

if __name__ == '__main__':
    main()


资源已将上传至资源库


http://www.niftyadmin.cn/n/1365780.html

相关文章

silverlight 无法发布 如何灵活配置IP

灵活配置IP可以有一个工具的 我为了比赛花了 两天工具做了一个配置Silverlight IP的小工具 可以参考 http://download.csdn.net/source/2714688转载于:https://www.cnblogs.com/zcy_soft/archive/2010/09/24/1833780.html

http协议-数据提交

编写程序实现通过有道或百度翻译url对用户输入数据进行翻译: 进入有道翻译或者百度翻译页面,找出页面向服务器提交待翻译数据使用的url; 使用浏览器分析工具分析相关需要提交的数据字段以及值,主要包括:(1&…

struts.custom.i18n.resources

struts.custom.i18n.resources 每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化 首先在struts.properties文件中加入以下内容:struts.custom.i18n.resourcesmessageResource或在struts.xml中加入<constant name"stru…

smtp协议——邮件发送

根据自己使用的邮件服务器&#xff0c;完成smtp协议的服务开启 给特定邮箱发送图文并茂的邮件&#xff0c;邮件正文包含的表格如下所示&#xff0c;显示的图片自己确定。 给特定邮箱发送邮件&#xff0c;邮件附件为“实验报告7 smtp协议 # -*- coding: utf-8 -*- ""&…

安装TensorFlow时出现Cannot uninstall 'enum34'等问题

Cannot uninstall enum34. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall. 你需要卸载enum34 输入以下命令即可&#xff1a; sudo pip install --ignore-installed en…

sudo python下import caffe出错然而python下没有问题

打开命令行 输入python 导包import sys 查看路径sys.path 出现一串路径其中会出现一条这样的路径“/home/your name/caffe/python” 第二&#xff1a; 输入sudo python 导包import sys 查看路径sys.path 出现一串路径并缺少上面关键一条路 任意地方新建一个文件---lu…

WINCE6.0+S3C2443的启动过程---eboot3

1.6 OEMAddresstable只是用来初始化一级页表&#xff0c;就是所谓的段&#xff08;section&#xff09;描述&#xff0c;每个段是1MB&#xff0c;分为4096个段&#xff0c;总共4G——虚拟内存空间4G就是由此而来。 并且这个OEMAddresstable可以用在查表法中用来转换虚拟地址、…

aise ImportError(str(msg) + ', please install the python3-tk package') ImportError: No module named

1. 进入超级管理员权限 sudo su 2. 安装 apt-get install python3-tk 但是&#xff1a;可能遇到 ubuntu提示E: 无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用) 解决办法&#xff1a; 方法1&#xff1a; ps -e | grep apt 显示&#xff1a; 116…