网络 ->【XHR】重新加载查看 json文件信息等 -> 获取 UR...">
美文网首页
爬虫小实验

爬虫小实验

作者: 是我真的是我 | 来源:发表于2019-10-15 18:09 被阅读0次

爬虫"模板"

推荐谷歌浏览器:F12 -> 网络 ->【XHR】重新加载查看 json文件信息等 -> 获取 URL + 响应头 + 参数

import requests
import json
from tqdm import trange

url = 'http://...'  # 点击要爬取的json文件,将会显示实际的url,全部复制即可

headers = {  # 请求头部信息,直接全部复制即可(注意不同类型的网页会不一样)
    "Accept": "......",
    "Accept-Language": "......",
    "Connection": "keep-alive",
    "Content-Length": "......",
    "Content-Type": "application/json",
    "User-Agent": "Mozilla/5.0 ......",
}

for index in trange(1, 11):  # 假如爬虫前10页,其中 params 是请求参数(根据实际完成参数字典)
    params = {"page": index, "rows": 100}  # 根据实际情况填写

    temp = requests.post(url, headers=headers, json=params)
    temp = temp.json()
    with open(r"dir_path\{}.json".format(index), "w", encoding="utf-8") as f:  # 保存
        json.dump(temp, f, ensure_ascii=False)

练习:获取网页信息

import urllib.request

response = urllib.request.urlopen('https://www.***.com')
html = response.read()
print(html)
# (显示忽略)

# 格式化
html = html.decode('utf-8')
print(html)
# (显示忽略)

练习:爬取图片并保存

import urllib.request

url = 'http://***./13928177_195158772185_2.jpg'
response = urllib.request.urlopen(url)
img = response.read()

with open('img.jpg', 'wb') as f:
    f.write(img)

print(response.geturl())
# http://***./13928177_195158772185_2.jpg
print(response.info())
# (各种信息)
print(response.code)
# 200

实战:有道翻译爬虫程序(仅供学习使用)

有道翻译有反爬虫机制,所以简单的爬肯定不行,还需要改你得到的POST请求的URL(否则会出现:{"errorCode":50})
我的URL:http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule
需要修改成:http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule
就是把_o去掉

import urllib.request
import urllib.parse
import json
import random

print('欢迎使用爬虫翻译程序!')

while True:
    text = input('请输入带翻译的内容(输入"q!"退出程序)')
    if text == 'q!':
        break

    # url
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'

    # data
    data = {}
    data['i'] = text
    data['from'] = 'AUTO'
    data['to'] = 'AUTO'
    data['smartresult'] = 'dict'
    data['client'] = 'fanyideskweb'
    data['salt'] = '15711301787743'
    data['sign'] = 'a802ca9da2f6603860996f28af946897'
    data['ts'] = '1571130178774'
    data['bv'] = 'e2a78ed30c66e16a857c5b6486a1d326'
    data['doctype'] = 'json'
    data['version'] = '2.1'
    data['keyfrom'] = 'fanyi.web'
    data['action'] = 'FY_BY_CLICKBUTTION'
    data = urllib.parse.urlencode(data).encode('UTF-8')

    # head
    head = {}
    head['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'

    # ip代理
    proxies = ['122.116.232.79:33452', '116.62.221.139:3128']
    proxy = random.choice(proxies)
    proxy_support = urllib.request.ProxyHandler({'http':proxy})
    opener = urllib.request.build_opener(proxy_support)
    urllib.request.install_opener(opener)

    # 爬取
    req = urllib.request.Request(url, data, head)
    response = urllib.request.urlopen(req)
    html = response.read().decode('UTF-8')

    # print(html)
    target = json.loads(html)
    print(target['translateResult'][0][0]['tgt'])

print('程序结束!')

运行展示

相关文章

  • 爬虫小实验

    爬虫"模板" 推荐谷歌浏览器:F12 -> 网络 ->【XHR】重新加载查看 json文件信息等 -> 获取 UR...

  • 使用Scrapy爬取大规模数据

    系统学习了解Python爬虫有20天时间了,做了一些爬虫小实验,如: 爬取51JOB岗位要求及信息 《当我们学Py...

  • 使用开放API获取网站数据——以豆瓣为例

    目录 实验内容实验环境实验过程 实验内容: 尝试使用网站开放的API,获取网站数据,了解除网络爬虫外获取高质量网站...

  • 爬取川大公管新闻实验报告

    实验名称:爬取川大公管新闻实验时间:2018/5/1实验人:梁福祯实验目的:运用已有的爬虫软件爬取出川大公管网站的...

  • phantomjs2.1 初体验

    上次看了一下scrapy1.1的新手指南 决定写个小爬虫实验一下 目标网站是http://www.dm5.com/...

  • 数据科学实践与学习索引

    Python 包 pandas 爬虫 小专栏—爬取广州二手房信息小专栏—爬虫模块化小专栏—广度优先爬虫小专栏—爬取...

  • Scrapinghub试用报告

    实验对象:scrapinghubs实验目的:通过体验爬虫工具,进一步加深对数据检索的认识 目录 scrapingh...

  • 小红书小程序爬虫抓包分析

    小红书 小程序爬虫 小红书爬虫 小红书是有 APP 和 小程序两个入口。今天介绍小红书小程序的爬虫。 主要流程是以...

  • 7个Python爬虫实战项目教程

    有很多小伙伴在开始学习Python的时候,都特别期待能用Python写一个爬虫脚本,实验楼上有不少python爬虫...

  • 名人名言数据练习

    1、实验目的 名人名言数据练习,学习用scrapy框架编写爬虫代码采集数据。 2、实验过程 (1)、新建spide...

网友评论

      本文标题:爬虫小实验

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