爬百度文字图片
  2U15lpegzuxq 2023年12月08日 20 0

import csv
import ssl
import time
ssl._create_default_https_context = ssl._create_unverified_context
import requests
from bs4 import BeautifulSoup  # 网页解析,获取数据
import re  # 正则表达式,进行文字匹配
import urllib.request, urllib.error  # 制定URL,获取网页数据
import xlwt  # 进行excel操作
import sqlite3  # 进行SQLite数据库操作
from urllib.parse import quote

from docx import Document
url1 ='https://hanyu.baidu.com/zici/s?from=aladdin&query=%E5%83%B5%E5%AD%97%E7%9A%84%E7%AC%94%E9%A1%BA&srcid=51368&wd='


findurl = re.compile(r'<img id="word_bishun" class="bishun" data-gif="(.*?)" src="/static/asset/img_wise/video-stroke.png"/>', re.S)  # 创建正则表达式对象,表示规则(字符串的模式)
def askURL(filename,url):
    head = {  # 模拟浏览器头部信息,向豆瓣服务器发送消息
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0",
        "Cookie": 'BAIDUID=F90D3D94B615E6BD2EFB2301DDE93EB2:SL=0:NR=10:FG=1; BIDUPSID=F90D3D94B615E6BD788B7046CCD244BD; PSTM=1687919213; H_WISE_SIDS=39710_39712_39780_39704_39794_39681_39662_39818_39843; MCITY=-53%3A; H_PS_PSSID=39712_39780_39704_39794_39681_39662_39898_39904_39819_39909_39935_39936_39932; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; ab_sr=1.0.1_NDlmMjFjYThlNWNkNTI3YzRkNThkYjAwMTZiYjAyYmY1Y2JlNWFjOTZmODY1NTVhZWZiZmM2NDFmODJiY2ExN2I2ZmJlYmQzMGExMWM3MzlkYjJlMTllZDczYWVmYTc1OWM5NzZiY2UyMjZhMjM1MzVlNWQyOThhMDY2ZDMxMTQ3YjM4NzJhODIyNzg0OWFlMGQ1OGZiODM0ZThjYzBkOA==; delPer=0; PSINO=2; RT="z=1&dm=baidu.com&si=fa7b5c6f-b4d1-41e4-9573-244e459f045c&ss=lpujepls&sl=z&tt=a65&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=5d5vw"; BA_HECTOR=8h8ga521ak0ga0a1252lala51in2gpk1r; ZFY=b0EPWrmXGHsRjLr9bRbN5TePki:AdemwaagNgQfkN5:AE:C; Hm_lvt_010e9ef9290225e88b64ebf20166c8c4=1701921595; Hm_lpvt_010e9ef9290225e88b64ebf20166c8c4=1701922522',
        "Host": "hanyu.baidu.com",
       # "Referer": "https://www.baidu.cn/",
        "Accept - Encoding":"gzip, deflate, br"

    }
    # 用户代理,表示告诉豆瓣服务器,我们是什么类型的机器、浏览器(本质上是告诉浏览器,我们可以接收什么水平的文件内容)

    request = urllib.request.Request(url, headers=head)
    html = ""
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode("utf-8")
        print(html)
        booklvl = re.findall(findurl, str(html))
        print(booklvl)


        response = requests.get(booklvl[0], stream=True)

        if response.status_code == 200:

            with open('./img/filename.gif', 'wb') as file:

                file.write(response.content)

            print('文件已保存到本地。')

        else:

            print('请求失败,无法保存文件。')
    except urllib.error.URLError as e:
        if hasattr(e, "code"):
            print(e.code)
        if hasattr(e, "reason"):
            print(e.reason)
    return html
s ='程'


# 打开Word文件
doc = Document('234下全部生字.docx')

# 获取文档中的所有段落
paragraphs = doc.paragraphs

# 初始化空列表
word_list = []

# 遍历每个段落,将每个汉字作为列表元素
# 遍历文档中的每个段落

for para in doc.paragraphs:

    # 将每个汉字作为一个元素添加到列表中

    for word in para.text.split():

        for char in word:

            word_list.append(char)

    # 打印列表
print(word_list)
for x in word_list:

    encoded_url = quote(x, encoding='utf-8', errors='strict')
    askURL(x,url1 + encoded_url)
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月08日 0

暂无评论

推荐阅读
  8l4CZpTOKa7P   2023年12月26日   32   0   0 htmlhtml
  dwHry2iKGG0I   2023年12月26日   28   0   0 githubgithubhtmlhtml
2U15lpegzuxq