猫史档案馆


【Python作品分享】爬虫2【作业帖】

用户:鸽子ECYRAZ鸽子ECYRAZ查看:0 回复:0 评论:0 创建时间:2021-07-18T16:51:24


【作品展示】

center_image

 

【作品介绍】

1

 

【作品源代码】

import requests
from bs4  import BeautifulSoup#解析文档,抓取数据
char=['A','B','C','D','E','F']
url = "http://ename.dict.cn/list/all/{}/{}"
#设置一个文档,下载所有的想要的数据
with open('name.txt','w',encoding='utf-8') as f:
    for i in char:
        res=requests.get(url.format(i,1))#格式化字符串函数str.format():将字符串之中{}替换为新的参数
        html=res.content#.content中间存的是字节码,.text存的是编码后的字符串。
        bs=BeautifulSoup(html,'html.parser')
        s=bs.find('div',{'class':'pager'})
        last_url=s.find_all('a')[-1].get('href')
        total=int(last_url[-1])
        for j in range(1,total+1):
            res=requests.get(url.format(i,j))
            html = res.content  # .content中间存的是字节码,.text存的是编码后的字符串。
            bs = BeautifulSoup(html, 'html.parser')
            for k in bs.find_all('tr')[1:]:
                temp=''
                for m in k.find_all('td'):
                    if m.string:
                        temp+=m.string+' '
                f.write(temp+'\n')
#MySql:小型数据库

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页