[爬虫] demo 以及使用etree示例

记载一下使用的爬虫代码
主要是活用 etree

etree 多跳转解析

from _chj.comm.pic import *
import requests
from lxml import etree
import pandas as pd
import urllib.parse
from requests_html import HTMLSession

session = HTMLSession()

class params:
    base_url="https://www.fangpi.net"
    sbase_url="https://www.fangpi.net/s/"
    fsing="tmp/sing.txt"
    furl_out="tmp/sing_url.sh"
    dsong = "tmp/songs"

def main():
    f1_get_files()
    f2_mk_wget()

def f2_mk_wget():
    chj_file.mkdir( params.dsong )
    exec_cmd( f" bash {params.furl_out} " )

def f1_get_files():
    fp = open(params.furl_out, "w")
    for i, line in enumerate( readlines( params.fsing ) ):
        arr = line.split()
        if len(arr) == 1:
            nm, uname = line, None
        else:
            nm, uname = line.split()
            uname = uname.strip()
            outurl, nm = get_link( nm.strip(), uname )
        if outurl is not None:
            #fp.write( f"{i} {outurl} {nm}\n")
            ss = f"wget -c '{outurl}' -O {params.dsong}/{i}-{nm}\n"
            fp.write( ss )

def get_link( query_nm, query_uname ):
    url = params.sbase_url +  urllib.parse.quote( query_nm )

    r = requests.get(url)
    tree = etree.HTML( r.text )
	r.close()
    items = tree.xpath(".//table[@class='table']/tbody/tr")

    for tr in items:
        #tds = tr.xpath(".//td")
        nm = tr[0].xpath("./a/text()")[0].strip()
        unm = tr[1].text
        urlsing = tr[2].xpath("./a/@href")[0]
        #print( nm, unm, urlsing )
        if nm == query_nm:
            if query_uname is None or unm == query_uname:
                url_sing = params.base_url + urlsing
                href, download = get_sing_url( url_sing )
                return href, download
    return None, None
    #df = pd.read_html(url, encoding='utf-8',header=0)
    # 这个会失去链接

def get_sing_url( url ):
    r = session.get(url) # 必须要渲染否则不行
    r.html.render()
    tree = etree.HTML( r.html.html )
    r.close()
    #print( r.html.html )
    a = tree.xpath(".//div[@class='input-group-append']/a[@id='btn-download-mp3']")[0]
    href = a.xpath("./@href")[0]
    download = a.xpath("./@download")[0]
    #print( href, download )
    #r = requests.get( href )
    return href, download

if __name__ == "__main__":
    main()
    

讲解

先获得list中合适的条目,然后解析获得最终音频的位置

普通网站爬取

先放上整体代码,再讲解

# -*- coding:utf-8 -* 
import urllib.request
from lxml import etree

root_url="http://www.itangyuan.com/"    
url=f"{root_url}/book/catalogue/14432108.html"
def main(): 
	
	# 第一步
    html = urllib.request.urlopen(url).read()
    tree = etree.HTML(html)
    
    links = tree.xpath(".//div[@class='catalog']/ul/li/a/@href")[2:]
    
    # 第二部
    for i, link in enumerate( links ):
        link = f"{root_url}{link}"

        html = urllib.request.urlopen(link).read()
        tree = etree.HTML(html).xpath(".//div[@class='section-main-con']")
        
        if len(tree) == 0: continue
        if len(tree) != 1: p("WARNING", link, len(len(tree)))
        tree = tree[0]
        
        title = tree.xpath(".//h1/text()")[0]
        content = "\n\n".join( tree.xpath(".//p/text()")[:-1] )
        
        with open(f"res/{i+1:03d}.{title}.txt", "w", encoding='utf-8') as fp:
            fp.write(content)
        
# 下面这个函数忽略
def main2(): 
    with open("虹猫蓝兔七侠传小说.md", "w", encoding="utf-8") as fpout:
        for fnm in glob.glob("res/*"):
            with open(fnm, encoding="utf-8") as fp:
                nm = fnm.split('\\')[1].split('.txt')[0]
                fpout.write(f"# {nm}\n")
                fpout.write(fp.read()+"\n")
 

if __name__ == '__main__': 
    main() 

讲解

  1. 分析网页 http://www.itangyuan.com/book/catalogue/14432108.html , 确定每个章节的网址
  2. 加载每个网址,分析其中一个页面,获得相应的内容

由于这个网页内容比较简单,直接 xpath 索引就全部获得了。

注意事项:
1)每次 xpath 完是个数组,可能应为是通过 class 索引缘故,未详细探索
2)用 utf-8 保存

使用 chrome 分析网站

想爬取一些数据
比如
https://tv.cctv.com/2019/12/31/VIDEOX9ykqMX1J0rlAhEmjeo191231.shtml

chrome 中分析发现一个链接
在这里插入图片描述

使用 ffmpeg 下载之后发现有问题 ( ffmpeg -i xx.m3u8 -c copy demo.mp4 ), 视频是模糊的

进一步分析网络包
先 clear 一下
在这里插入图片描述

刷新一下页面,分析包,然后发现下面那个请求,获得一个本质上是 json 的 url
在这里插入图片描述

可以在这个 url 中 分析得到
在这里插入图片描述
不过chrome 默认是没有装 jsonview插件的可以装一下

jsonView

小的实例汇总

lyric

divs = tree.xpath(".//div[@label-module='para*']")
for e in divs: print( e.text)
### 爬虫课程设计项目源码 GitHub 示例教程 #### 一、GitHub模拟登录爬虫实例 对于希望了解如何利用Python编写能够处理登录验证的爬虫程序的学生来说,一个很好的起点是从模拟登录GitHub入手。此过程涉及发送POST请求来提交用户名和密码,并解析返回的内容以获取目标数据。 ```python import requests from lxml import etree def login_github(username, password): session = requests.Session() # 获取 authenticity_token response = session.get('https://github.com/login') selector = etree.HTML(response.text) token = selector.xpath('//input[@name="authenticity_token"]/@value')[0] post_data = { 'commit': 'Sign in', 'utf8': '✓', 'authenticity_token': token, 'login': username, 'password': password } headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'} url_login = "https://github.com/session" res = session.post(url_login,data=post_data,headers=headers) if res.status_code == 200: print("Login successful!") else: print("Failed to log in.") return session ``` 上述代码展示了如何创建会话并执行带有适当参数的HTTP POST请求以完成登录操作[^1]。 #### 二、适合初学者的开源爬虫项目推荐 为了帮助学生更好地理解爬虫的工作原理以及实际应用中的注意事项,在选择合适的课程设计题目时可以考虑参与一些较为简单但又具有代表性的开源项目。以下是几个被广泛认可且非常适合新手尝试的Python爬虫项目: - **Scrapy Tutorial**: 官方提供的入门级文档,指导用户构建自己的第一个Spider。 - **PyQuery Example**: 使用pyquery库轻松抓取网页内容的例子。 - **BeautifulSoup Demo**: 结合requests库与beautifulsoup4进行HTML解析的教学案例。 以上提到的一些优秀资源不仅提供了详尽说明还附带了完整的源代码供学习者参考[^2][^3]。 #### 三、具体功能实现——文件下载器 除了基本的信息采集外,有时还需要从远程服务器上获取特定类型的文件并将它们保存到本地磁盘中。这里给出了一种不依赖额外框架(如Selenium)的方法来进行此类任务的操作示范。 ```python import os import urllib.request as req def download_file(file_url, save_path='./downloads'): filename = file_url.split('/')[-1] filepath = os.path.join(save_path,filename) try: if not os.path.exists(save_path):os.makedirs(save_path) req.urlretrieve(file_url,filepath) print(f'{filename} has been downloaded successfully.') except Exception as e: print(e) if __name__=='__main__': test_url='http://example.com/path/to/file.txt' download_file(test_url) ``` 这段脚本定义了一个名为`download_file()`的功能函数,它接受两个参数:一个是待下载的目标链接地址;另一个则是指定用于存放所获资料的目的路径,默认情况下会在当前目录下建立一个新的子文件夹作为仓库位置[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值