useragentstring.com 网站几乎廊括了所有的User-Agent,刚学了scrapy,打算那它练手,把上面的 user-agent 爬取下来。

本文只爬取常见的 FireFox, Chrome, Opera, Safri, Internet Explorer

一、创建爬虫项目

1.创建爬虫项目useragent
$ scrapy startproject useragent
2.进入项目目录
$ cd useragent
3.生成爬虫文件 ua

这一步不是必须的,不过有了就方便些

$ scrapy genspider ua useragentstring.com

二、编辑 item 文件

# useragent\items.py
import scrapy class UseragentItem(scrapy.Item):
# define the fields for your item here like:
ua_name = scrapy.Field()
ua_string = scrapy.Field()

三、编辑爬虫文件

# useragent\spiders\ua.py 

import scrapy

from useragent.items import UseragentItem

class UaSpider(scrapy.Spider):
name = "ua"
allowed_domains = ["useragentstring.com"]
start_urls = (
'http://www.useragentstring.com/pages/useragentstring.php?name=Firefox',
'http://www.useragentstring.com/pages/useragentstring.php?name=Internet+Explorer',
'http://www.useragentstring.com/pages/useragentstring.php?name=Opera',
'http://www.useragentstring.com/pages/useragentstring.php?name=Safari',
'http://www.useragentstring.com/pages/useragentstring.php?name=Chrome',
) def parse(self, response):
ua_name = response.url.splite('=')[-1]
for ua_string in response.xpath('//li/a/text()').extract():
item = UseragentItem()
item['ua_name'] = ua_name
item['ua_string'] = ua_string.strip()
yield item

四、运行爬虫

通过参数-o,控制爬虫输出为 json 文件

$ scrapy crawl ua -o item.json

结果如图:

看起来没有得到想要的结果,注意到那个robot.txt。我猜测可能是网站禁止爬虫

猜的对不对先不管,先模拟浏览器再说,给所有的 request 添加 headers:

# useragent\spiders\ua.py 

import scrapy

from useragent.items import UseragentItem

class UaSpider(scrapy.Spider):
name = "ua"
allowed_domains = ["useragentstring.com"]
start_urls = (
'http://www.useragentstring.com/pages/useragentstring.php?name=Firefox',
'http://www.useragentstring.com/pages/useragentstring.php?name=Internet+Explorer',
'http://www.useragentstring.com/pages/useragentstring.php?name=Opera',
'http://www.useragentstring.com/pages/useragentstring.php?name=Safari',
'http://www.useragentstring.com/pages/useragentstring.php?name=Chrome',
) # 在所有的请求发生之前执行
def start_requests(self):
for url in self.start_urls:
headers = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"}
yield scrapy.Request(url, callback=self.parse, headers=headers) def parse(self, response):
ua_name = response.url.split('=')[-1]
for ua_string in response.xpath('//li/a/text()').extract():
item = UseragentItem()
item['ua_name'] = ua_name
item['ua_string'] = ua_string.strip()
yield item

在运行,OK了!

效果图如下:

好了,以后不愁没有 User Agent用了。

scrapy 爬取 useragent的更多相关文章

  1. scrapy爬取全部知乎用户信息

    # -*- coding: utf-8 -*- # scrapy爬取全部知乎用户信息 # 1:是否遵守robbots_txt协议改为False # 2: 加入爬取所需的headers: user-ag ...

  2. Scrapy爬取Ajax(异步加载)网页实例——简书付费连载

    这两天学习了Scrapy爬虫框架的基本使用,练习的例子爬取的都是传统的直接加载完网页的内容,就想试试爬取用Ajax技术加载的网页. 这里以简书里的优选连载网页为例分享一下我的爬取过程. 网址为: ht ...

  3. Scrapy爬取静态页面

    Scrapy爬取静态页面 安装Scrapy框架: Scrapy是python下一个非常有用的一个爬虫框架 Pycharm下: 搜索Scrapy库添加进项目即可 终端下: #python2 sudo p ...

  4. 用scrapy爬取京东的数据

    本文目的是使用scrapy爬取京东上所有的手机数据,并将数据保存到MongoDB中. 一.项目介绍 主要目标 1.使用scrapy爬取京东上所有的手机数据 2.将爬取的数据存储到MongoDB 环境 ...

  5. Scrapy爬取美女图片第四集 突破反爬虫(上)

     本周又和大家见面了,首先说一下我最近正在做和将要做的一些事情.(我的新书<Python爬虫开发与项目实战>出版了,大家可以看一下样章) 技术方面的事情:本次端午假期没有休息,正在使用fl ...

  6. scrapy爬取美女图片

    使用scrapy爬取整个网站的图片数据.并且使用 CrawlerProcess 启动. 1 # -*- coding: utf-8 -* 2 import scrapy 3 import reques ...

  7. 以豌豆荚为例,用 Scrapy 爬取分类多级页面

    本文转载自以下网站:以豌豆荚为例,用 Scrapy 爬取分类多级页面 https://www.makcyun.top/web_scraping_withpython17.html 需要学习的地方: 1 ...

  8. python scrapy爬取HBS 汉堡南美航运公司柜号信息

    下面分享个scrapy的例子 利用scrapy爬取HBS 船公司柜号信息 1.前期准备 查询提单号下的柜号有哪些,主要是在下面的网站上,输入提单号,然后点击查询 https://www.hamburg ...

  9. 用scrapy爬取搜狗Lofter图片

    用scrapy爬取搜狗Lofter图片 # -*- coding: utf-8 -*- import json import scrapy from scrapy.http import Reques ...

随机推荐

  1. 一个线程加一运算,一个线程做减一运算,多个线程同时交替运行--synchronized

    使用synchronized package com.pb.thread.demo5; /**使用synchronized * 一个线程加一运算,一个线程做减法运算,多个线程同时交替运行 * * @a ...

  2. iOS 在制作framework时候对aggregate的配置

    # Sets the target folders and the final framework product.# 如果工程名称和Framework的Target名称不一样的话,要自定义FMKNA ...

  3. 后台API服务的设计考虑

    我在<写在最前>里说过,后台API的文档至关重要.不过,文档只是外在表现形式,设计才是真正的灵魂.我在这篇博文主要介绍的就是我在后台开发过程中,设计API时的考虑.我只说他是考虑,因为很多 ...

  4. android5.x新特性之Tinting

    Android5.X对图形操作上有更多的功能.下面来看看Tinting(着色) Tinting的使用非常简单,几乎 没什么好说的,只要在xml中配置好tint和tintMode即可.直接看实际例子吧. ...

  5. JavaScript Patterns 3.4 Array Literal

    Array Literal Syntax To avoid potential errors when creating dynamic arrays at runtime, it's much sa ...

  6. RESTful API的设计与开发

    自己做过关于RESTful API的培训,下载

  7. Showing progress bar in a status bar pane

    在工具卡显示进度条,原文链接:http://www.codeproject.com/Articles/35/Showing-progress-bar-in-a-status-bar-pane 1.构造 ...

  8. flask中'bool' object has no attribute '__call__'问题

    #写flask时报错 <ul class="nav navbar-nav"> <li><a href="/">Home< ...

  9. ORA-29857: domain indexes and/or secondary objects

    dmp导入的时候出了问题,想把表空间和用户删除重建,然后再重新导入,却在删除表空间时报错:   > ORA-29857: domain indexes and/or secondary obje ...

  10. URAL 1430 Crime and Punishment

    Crime and Punishment Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...