python3.7简单的爬虫实例详解

yipeiwu_com5年前Python爬虫

python3.7简单的爬虫,具体代码如下所示:

#https://www.runoob.com/w3cnote/python-spider-intro.html

#Python 爬虫介绍

import urllib.parse
import urllib.request
from http import cookiejar
url = "http://www.baidu.com"
response1 = urllib.request.urlopen(url)
print("第一种方法")
#获取状态码,200表示成功
print(response1.getcode())
#获取网页内容的长度
print(str(response1.read()))
print(len(response1.read()))
print("第二种方法")
request = urllib.request.Request(url)
#模拟Mozilla浏览器进行爬虫
request.add_header("user-agent","Mozilla/5.0")
response2 = urllib.request.urlopen(request)
print(response2.getcode())
print(len(response2.read()))
print("第三种方法")
cookie = cookiejar.CookieJar()
#加入urllib2处理cookie的能力#
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))
urllib.request.install_opener(opener)
response3 = urllib.request.urlopen(url)
print(response3.getcode())
print(len(response3.read()))
print(cookie)
code: https://github.com/pascal19821003/python
path: python/study/tutorial/pachong/1.py

总结

以上所述是小编给大家介绍的python3.7简单的爬虫实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

python爬虫自动创建文件夹的功能

该爬虫应用了创建文件夹的功能: #file setting folder_path = "D:/spider_things/2016.4.6/" + file_name +"/" i...

如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求

如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求

网站经常会被各种爬虫光顾,有的是搜索引擎爬虫,有的不是,通常情况下这些爬虫都有UserAgent,而我们知道UserAgent是可以伪装的,UserAgent的本质是Http请求头中的一...

Python爬虫包BeautifulSoup实例(三)

一步一步构建一个爬虫实例,抓取糗事百科的段子 先不用beautifulsoup包来进行解析 第一步,访问网址并抓取源码 # -*- coding: utf-8 -*- # @Auth...

python定向爬取淘宝商品价格

python爬虫学习之定向爬取淘宝商品价格,供大家参考,具体内容如下 import requests import re def getHTMLText(url): try:...

python多线程抓取天涯帖子内容示例

使用re, urllib, threading 多线程抓取天涯帖子内容,设置url为需抓取的天涯帖子的第一页,设置file_name为下载后的文件名 复制代码 代码如下:#coding:...