美文网首页
5.简单的爬虫编写

5.简单的爬虫编写

作者: M_小七 | 来源:发表于2020-07-16 18:28 被阅读0次

爬取豆瓣出版社

import urllib.request
import re
# 发现会报错,当使用urllib模块访问https网站时,由于需要提交表单,而python3默认是不提交表单的,所以这时只需在代码中加上以下代码
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
data = urllib.request.urlopen("https://read.douban.com/provider/all").read().decode("utf-8")
pettern = '<div class="name">(.*?)</div>'
result = re.compile(pettern).findall(data)
print(f'出版社有:{result}')

此时报错 HTTP Error 418
“HTTP Error 418:”应该是网站的反爬程序返回的。
在使用浏览器访问网站时,访问请求中包含请求头。检测请求头是常见的反爬虫策略。
服务器通过检测请求头判断这次请求是不是人为的。
在程序上加入请求头,这样服务器就会认为这是一个从浏览器发出的人为请求:
修改程序

import urllib.request
import re
import ssl

ssl._create_default_https_context = ssl._create_unverified_context
url='https://read.douban.com/provider/all'
#请求头
herders={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1;WOW64) AppleWebKit/537.36 (KHTML,like GeCKO) Chrome/45.0.2454.85 Safari/537.36 115Broswer/6.0.3',
    'Referer':'https://read.douban.com/provider/all',
    'Connection':'keep-alive'}
req = urllib.request.Request(url,headers=herders)
response = urllib.request.urlopen(req)
data = response.read().decode('utf8')
pettern = '<div class="name">(.*?)</div>'
result = re.compile(pettern).findall(data)
print(f'出版社有:{result}')

现在我们成功爬取到所有出版社,我们将爬取的出版社名称保存到excel里

import urllib.request
import re
import ssl
import xlwt
# 导入

from openpyxl import workbook  # 写入Excel表所用

ssl._create_default_https_context = ssl._create_unverified_context
url='https://read.douban.com/provider/all'
#请求头
herders={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1;WOW64) AppleWebKit/537.36 (KHTML,like GeCKO) Chrome/45.0.2454.85 Safari/537.36 115Broswer/6.0.3',
    'Referer':'https://read.douban.com/provider/all',
    'Connection':'keep-alive'}
req = urllib.request.Request(url,headers=herders)
response = urllib.request.urlopen(req)
data = response.read().decode('utf8')
pettern = '<div class="name">(.*?)</div>'
result = re.compile(pettern).findall(data)

# 创建工作workbook
workbook = xlwt.Workbook()

# 创建工作表worksheet,填入表名
worksheet = workbook.add_sheet('表名')

# 在表中写入相应的数据
for i in range(len(result)):
    worksheet.write(i, 0, label=result[i])

# 保存表
workbook.save('出版社.xls')

相关文章

  • 5.简单的爬虫编写

    爬取豆瓣出版社 此时报错 HTTP Error 418“HTTP Error 418:”应该是网站的反爬程序返回的...

  • 爬取网站

    1.新建项目 2.新建爬虫 3.新建python文件main 4.编写item文件定义需要抓取的字段名 5.编写爬...

  • 山东大学-VirtualJudge-总结1

    在这个周根据小组进度安排,我主要学习了Python爬虫的编写,学习主要参考:python实现简单爬虫功能 根据博客...

  • Scrapy的使用

    创建一个Scrapy项目 Scrapy的项目结构 spiders:编写爬虫的目录 爬虫的编写规则 运行你的爬虫

  • 5.简单爬虫------------使用selenium

    该文章仅供学习,如有错误,欢迎指出 这里列出了文档内的大部分可以使用的selenium代码selenium中文文档...

  • Python新闻爬虫

    新闻爬虫编写

  • PY爬虫极速学习 (九)简单爬虫编写

    然后只有vx二维码了 那就爬二维码吧我是360浏览器 基本都差不多 F12 然后这样点击可以直接跳到该图片的代码位...

  • 爬虫基础

    在编写一个爬虫之前,来学习下爬虫的基础知识: 爬虫:简单的可以理解为模拟浏览器向服务器发送HTTP请求,服务器接收...

  • Python学习笔记2——认识网页

    1、认识爬虫 爬虫简单来说,就是按照一定的规则,自动地抓取互联网上所需要的信息的程序或者脚本。 用Python编写...

  • 使用 Scrapy 框架爬取自己的简书文章

    先爬取自己的简书页面来试下手。简单地获取个人简书页面的文章和链接。 创建项目 创建爬虫 编写爬虫 先启动 chro...

网友评论

      本文标题:5.简单的爬虫编写

      本文链接:https://www.haomeiwen.com/subject/yixphktx.html