requests_html快速爬取数据

1、安装

只支持python3.6及以上版本

pip install requests-html

2、使用方法

2.1构造请求

from requests_html import HTMLSession
session = HTMLSession()
# 用的文章阅读网的页面
r = session.get('http://www.duwenzhang.com/wenzhang/gaoxiaowenzhang/youmo/20130608/257733.html')

2.1.1获取本页面所有的链接并返回一个列表, 保留了url在页面中原本的形式(已经自动去掉了html标签):

# 获取页面上的所有链接。
all_links = r.html.links
print(all_links)

2.1.2获取本页面所有的链接并返回一个列表, 自动将url转换为绝对路径形式(已经自动去掉了html标签):

# 获取页面上的所有链接,以绝对路径的方式。
all_absolute_links = r.html.absolute_links
print(all_absolute_links)

2.1.3通过css选择器选取Element对象**

    # 通过CSS找到新闻标签也就是css选择器形式
    news = r.html.find('.daohang > a')
    print(news)
    
	for new in news:
	    # 获取一个Element对象内的文本内容
	    print(new.text)
	    # 获取一个Element对象的所有attributes
	    print(new.attrs)
	    # 渲染出一个Element对象的HTML内容也就是从定位的地方打印所有代码:
	    print(new.html)
	    # 获取Element对象内的特定子Element对象
	    print(new.find('a'))
    # 在获取的页面中查找文本匹配的是大括号中(纯文本专用)
    news = r.html.search('悟空你{}说')[0]
    print(news)
    # 直接打印文本标签所有内容(纯文本)
    sel = 'h3 > del'
    print(r.html.find(sel, first=True).text)
    # 你也可以获取到只包含某些文本的Element对象
    news = r.html.find('.daohang > a', containing='首页')
    print(news)

2.1.4通过xpath选择器选取Element对象

# 提取文本
t = r.html.xpath('//*[@id="wenzhangziti"]')
for i in t:    
	print(i.text)

2.2通过html获取渲染JavaScript渲染的文本

from requests_html import HTML
doc = """<a href='http://www.duwenzhang.com/wenzhang/gaoxiaowenzhang/youmo/20130608/257733.html'>"""
html = HTML(html=doc)
script = """
        () => {
            return {
                width: document.documentElement.clientWidth,
                height: document.documentElement.clientHeight,
                deviceScaleFactor: window.devicePixelRatio,
            }
        }
    """
val = html.render(script=script, reload=False)
print(val)

demo

 import requests
    from requests_html import HTMLSession
    session = HTMLSession()
    """
    下载图片
    """
    
    # 保存图片到bg/目录
    
    def save_image(url, title):
        img_response = requests.get(url)
        with open('./bg/'+title+'.jpg', 'wb') as file:
            file.write(img_response.content)
    
    # 背景图片地址,这里选择1920*1080的背景图片
    
    url = "http://www.win4000.com/wallpaper_2358_0_10_1.html"
    
    session = HTMLSession()
    r = session.get(url)
    
    # 查找页面中背景图,找到链接,访问查看大图,并获取大图地址
    
    items_img = r.html.find('ul.clearfix > li > a')
    for img in items_img:
        img_url = img.attrs['href']
        if "/wallpaper_detail" in img_url:
            r = session.get(img_url)
            item_img = r.html.find('img.pic-large', first=True)
            url = item_img.attrs['src']
            title = item_img.attrs['title']
            print(url+title)
            save_image(url, title)

好的,下面是一个简单的Python爬虫爬取网页数据并解析数据的教程: 1. 确定目标网站和要爬取的信息 首先,需要确定要爬取的网站和要提取的信息。可以使用Pythonrequests库向网站发送HTTP请求获取HTML源代码,并使用BeautifulSoup库解析HTML文档获取目标数据。 例如,我们要爬取CSDN博客的文章标题和链接,可以先打开CSDN博客主页,右键查看网页源代码,找到文章标题和链接所在的HTML标签。 2. 发送HTTP请求获取HTML源代码 接下来,使用Pythonrequests库向网站发送HTTP请求,获取HTML源代码。 ``` import requests url = 'https://blog.csdn.net/' response = requests.get(url) html = response.text ``` 3. 解析HTML文档获取目标数据 使用BeautifulSoup库解析HTML文档,获取目标数据。 ``` from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'html.parser') titles = soup.find_all('div', class_='title') for title in titles: link = title.find('a').get('href') title_text = title.find('a').text.strip() print(title_text, link) ``` 上述代码中,通过`find_all`方法找到所有class属性为"title"的div标签,然后在每个div标签中找到第一个a标签,获取链接和标题文本。 4. 完整代码 ``` import requests from bs4 import BeautifulSoup url = 'https://blog.csdn.net/' response = requests.get(url) html = response.text soup = BeautifulSoup(html, 'html.parser') titles = soup.find_all('div', class_='title') for title in titles: link = title.find('a').get('href') title_text = title.find('a').text.strip() print(title_text, link) ``` 以上就是一个简单的Python爬虫爬取网页数据并解析数据的教程。需要注意的是,在爬取网站数据时要遵守网站的爬虫协议,避免被网站封禁IP。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值