Xpath多线程爬取Taptap新游预约信息

  Python用来爬取网页上的相关信息很方便,比如抓取相关网站的评论,下载链接,图片,模拟登陆等等,今天初步分享一个爬取网页相关信息的一个例子。

爬虫步骤:

 1.获取想要爬取网页的源代码。
 2.解析这些代码,筛选出想要的信息。
 3.将想要的内容保存到文件中。

举例:

 目标网页:https://www.taptap.com/category/e378?page=1
 爬取信息:【游戏名称】,【游戏种类】,【游戏链接】
第一步:通过requests获得网页的源代码。

Eg1:

import requests #导入requests
html=request.get(url)
第二步:通过etree把这些代码解析成xpath能够使用的格式,通过xpath爬取内容。

Eg2:

from lxml import etree #导入etree
selector = etree.HTML(html.text)

  完成上面操作后我们就可以用xpath进行读取代码了,找到我们想要的内容位置。通过chrome的开发者工具定位信息位置.
  {xpath使用方法:
    //定位根节点
    /往下层寻找
    提取文本内容:/text()
    提取某个属性的内容:/@XXX}


Xpath多线程爬取Taptap新游预约信息_第1张图片
data = selector.xpath('//div[@class="taptap-app-item"]')

  这里采用了先抓大再抓小的步骤,我们先把所有的游戏都抓下来,再在这些游戏里提取我们想要的每个游戏中的信息。
在'div[@class="taptap-app-item"]'中我们在具体定位。


Xpath多线程爬取Taptap新游预约信息_第2张图片

【游戏名称】:

'div[@class="app-item-caption"]/a[@class="item-caption-title flex-text-overflow"]/h4[@class="flex-text"]/text()’

【游戏种类】:

'div[@class="app-item caption"]/span[@class="item-caption-label"]/a/text()'

【游戏链接】:

'a/@href'

这里注意下,没有写根节点//的原因是我们在'//div[@class="taptap-app-item"]'查找的,所以不需要写//。
我们想要的信息爬取完了我们要将数据存起来,并且在对应的内容前呢加上标识。

第三步:定义存储的格式,将爬取内容进行存储。

Eg3:

def towrite(contentdict):
    f.towritelines(u'游戏名称:’ + str(contentdict(game_name)) +'\n' )
    f.towritelines(u'游戏种类:’ + str(contentdict(game_kind)) +'\n' )
    f.towritelines(u'游戏链接:’ + str(contentdict(game_link)) +'\n\n' )

  当我们输入f = open('content.txt', 'a',encoding='utf-8')时,就会将爬取下来的内容存储到content.txt中了。

完整的代码
from lxml import etree
from multiprocessing.dummy import Pool as ThreadPool #(多线程)
import requests
def towrite(contentdict):
    f.writelines(u'游戏名称:' + str(contentdict['game_name']) + '\n')
    f.writelines(u'游戏种类:' + str(contentdict['game_kind']) + '\n')
    f.writelines(u'游戏链接:' + str(contentdict['game_link']) + '\n\n')
def spider(url):
    html = requests.get(url)
    selector = etree.HTML(html.text)
    data = selector.xpath('//div[@class="taptap-app-item"]')
    item = {}
    for each in data:
        game_name = each.xpath('div[@class="app-item-caption"]/a[@class="item-caption-title flex-text-overflow"]/h4[@class="flex-text"]/text()')[0]
        game_kind = each.xpath('div[@class="app-item-caption"]/span[@class="item-caption-label"]/a/text()')[0]
        game_link = each.xpath('a/@href')[0]
        print(game_name)
        print(game_kind)
        print(game_link)
        item['game_name'] = game_name
        item['game_kind'] = game_kind
        item['game_link'] = game_link
        towrite(item)
if __name__=="__main__": #当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行。
    Pool = ThreadPool(4)#根据电脑的核数写的效率高,本电脑是4核的所以写4,4个线程同时进行,如过不写参数默认的是电脑的核数.
    f = open('content.txt', 'a',encoding='utf-8')
    page = []
    for i in range(1,20): #(爬取1-19页内容)
        newpage = 'https://www.taptap.com/category/e378?page=' + str(i)
        page.append(newpage)
    results = Pool.map(spider,page) #Pool.map 是多线路同时进行的意思
    Pool.close() 
    f.close()
运行结果:
Xpath多线程爬取Taptap新游预约信息_第3张图片

你可能感兴趣的:(Xpath多线程爬取Taptap新游预约信息)