【Python】学习笔记总结7(简单爬虫)

七、Python简单爬虫

1.重要知识与技能

重要知识与技能:

  1. 使用HTML与CSS制作网页文件
  2. 使用re正则表达式抓取网页文件
  3. 使用requests获取网站内容
  4. 使用re正则表达式提取数据
  5. 使用xPath工具提取数据
  6. 使用BeautifulSoup工具

2.使用re表达式抓取网页文件

import re
myFile = open('Index.html','r',encoding='UTF-8')
myContent = myFile.read()
myFile.close()
#myPatten = "<li>(.*)</li>"
myPatten2 = "([a-zA-Z0-9_\.-]+@[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)+)"
mylist =  re.findall(myPatten2,myContent)
print(mylist)

3.使用requests抓取网页

import requests
myURL = 'https://www.3dmgame.com'
myContent = requests.get(myURL).content.decode('UTF-8')

4.使用re正则表达式提取数据

def Get3DMNews_WithRE():
    '''
    得到3DM网站的新闻内容
    :return: 获取的新闻内容
    '''
    import requests
    import re
    myURL = 'https://www.3dmgame.com'
    myContent = requests.get(myURL).content.decode('UTF-8')
    myPartten = '<a href="(.*)" target="_blank"  >(.*)</a>\n                <span>(.*)</span>'
    myList = re.findall(myPartten,myContent)
    for item in myList :
        myNews = {}
        myNews['title'] = item[0]
        myNews['herf'] = item[1]
        myNews['time'] = item[2]
        print(myNews)
        pass
    pass

5.使用xPath工具提取数据

def Get3DMNews_WithXPATH():
    '''
    得到3DM网站的新闻内容
    :return: 获取的新闻内容
    '''
    import requests
    from lxml import html
    myURL = 'https://www.3dmgame.com'
    myContent = requests.get(myURL).content.decode('UTF-8')
    etree = html.etree
    eTreeHtml = etree.HTML(myContent)
    myList = eTreeHtml.xpath("//li")
    for item in myList :
        myNews = {}
        myNews['title'] = item.xpath('./a')[0].text
        myNews['herf'] = item.xpath('./a/@href')[0]
        myNews['time'] = item.xpath('./span')[0].text
        print(myNews)
        pass
    pass

6.使用BeautifulSoup工具

def Get3DMNews_WithBeautifulSoup():
    '''
    得到3DM网站的新闻内容
    :return: 获取的新闻内容
    '''
    import requests
    from bs4 import BeautifulSoup
    myURL = 'https://www.3dmgame.com'
    myContent = requests.get(myURL).content.decode('UTF-8')
    bsHtml = BeautifulSoup(myContent,'html5lib')
    myList = bsHtml.find_all('div')[10].find_all('div')[8].find_all('div')[91].find_all('li')
    for item in myList :
        myNews = {}
        myNews['title'] = item.find('a').get_text()
        myNews['herf'] = item.find('a').get('href')
        myNews['time'] = item.find('span').get_text()
        print(myNews)
        pass
    pass
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LetsonH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值