pyhton爬虫--爬取扇贝单词

“”"

思路

爬取所有的单词
分页爬取
写入一个文件里面
然后写入json文件中
最后写入文档
“”"

import os
import requests
from lxml import etree
def python(page):
    #起始的url
    for page in range(1,int(page)+1):
        path='./english/python'
        if not os.path.exists(path):
            os.makedirs(path)
        base_url="https://www.shanbay.com/wordlist/110521/232414/?page={}".format(page)
        response=requests.get(base_url)
        html=response.text
        # print(html)
        #将字符串格式转换成xml格式
        html_xml=etree.HTML(html)
        #获取所有的单词
        python_list=html_xml.xpath('//strong/text()')
        print(python_list)
        #获取所有的中文解释
        china_list=html_xml.xpath('//td[@class="span10"]/text()')
        print(china_list)
        big_dic={
            '{}页数'.format(page):{}
        }
        for i in range(len(china_list)):
            dic = {}
            dic['英文']=python_list[i]
            dic['翻译']=china_list[i]
            # print(dic)
            # print(type(dic))#查看数据类型
            big_dic.get('{}页数'.format(page))[python_list[i]]=china_list[i]
        import json
        path1=path+str(page)+'.txt'
        with open(path1,'w',encoding='utf-8') as f:
            f.write(json.dumps(str(big_dic)))
if __name__ == '__main__':
    page=input('请输入要爬取的页数')
    python(page)

第二种方式

‘’’
爬取扇贝网python必背词汇表
接口地址:https://www.shanbay.com/wordlist/110521/232414/
要求:获取所有的python词汇数据,形成字典,然后存储数据

思路:
    第一页:https://www.shanbay.com/wordlist/110521/232414/
    第二页:https://www.shanbay.com/wordlist/110521/232414/?page=2

‘’’

import requests,re,os,json
from lxml import etree
class Shanbei:
    def __call__(self, *args, **kwargs):
        self.get_xml(*args)

    def get_xml(self,pages):
        '''
    获取扇贝英语python页 内容 转换为xml
    :return:获取扇贝英语python页的xml
    '''
    base_url = 'https://www.shanbay.com/wordlist/110521/232414/?page='

    for page in range(1,pages+1):
        base_html = requests.get('https://www.shanbay.com/wordlist/110521/232414/?page=1').text
        base_xml = etree.HTML(requests.get(base_url+str(page)).text)
        pages = page

        # print(base_xml)

        with open('贝壳应英语.html','w',encoding='utf-8') as f :
            f.write(base_html)
        self.get_python(base_html,base_xml,pages)


def get_python(self,base_html,base_xml,pages):
    '''
    处理python页面数据
    :param base_xml:
    :return:
    '''
    #获取标题
    # main_title = base_xml.xpath("/html/body/div[@class='container main-body ']/div[@class='row']/div[@class='span8']/div[@class='row'][1]/div[@class='span8']/div[@class='well']/div[@class='row']/div[@class='span6']/h4/text()")
    #胆子获取
    worlds_rule = re.compile(r'<h4>(.*?)</h4>',re.S)
    main_title = worlds_rule.findall(base_html)

    get_words = re.compile(r'[a-z][A-Z]+',re.I)
    world_title = (get_words.findall(main_title[0]))[0]

    # print(base_html)
    print(world_title)

    # #获取本页词条数量
    trs_length = len(base_xml.xpath("/html/body/div[@class='container main-body ']/div[@class='row']/div[@class='span8']/div[@class='row'][2]/div[@class='span8']/table[@class='table table-bordered table-striped']/tbody/tr[@class='row']"))
    print(trs_length)

    #准备大字典
    shell_word_dict = {
        '必备单词{}'.format(world_title):{},
    }

    #根据词条数量获取循环次数
    for line in range(int(trs_length)):
        word = base_xml.xpath("/html/body/div[@class='container main-body ']/div[@class='row']/div[@class='span8']/div[@class='row'][2]/div[@class='span8']/table[@class='table table-bordered table-striped']/tbody/tr[@class='row'][{}]/td[@class='span2']/strong/text()".format(line+1))
        word_description = base_xml.xpath("/html/body/div[@class='container main-body ']/div[@class='row']/div[@class='span8']/div[@class='row'][2]/div[@class='span8']/table[@class='table table-bordered table-striped']/tbody/tr[@class='row'][{}]/td[@class='span10']/text()".format(line+1))
        # print(word,word_description)

        shell_word_dict.get('必备单词{}'.format(world_title))[word[0]] = word_description[0]
        print(shell_word_dict)

    #储存文件
    path = './扇贝单词/'+'第'+str(pages)+'页' +'/'
    print(path)
    if not os.path.exists(path):
        os.makedirs(path)
    file_path = path+'第'+str(pages)+'页'

    with open(file_path,'w',encoding='utf-8')as f:
        f.write(json.dumps(shell_word_dict))


if __name__ == '__main__':
    # pages = int(input('请输入需要爬取的页数'))
    pages = 3
    shanbei = Shanbei()
    shanbei(pages)
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下如何使用 Python 爬虫房产数据并保存到本地。 首先,您需要选择一个合适的网站来进行。例如,可以选择链家网、安居客等房产网站。然后,您需要分析该网站的页面结构和数据请求方式,确定需要哪些数据,以及如何获这些数据。 接着,您可以使用 Python 的 requests 库来发送 HTTP 请求,获网页内容。然后,可以使用 BeautifulSoup 库来解析网页内容,提需要的数据。 最后,您可以使用 Python 的 Pandas 库将数据保存到本地的 Excel 或 CSV 文件中。以下是一个示例代码: ```python import requests from bs4 import BeautifulSoup import pandas as pd # 发送 HTTP 请求获网页内容 url = 'https://bj.lianjia.com/ershoufang/' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.content, 'html.parser') # 解析网页内容,提需要的数据 house_list = soup.find_all('div', {'class': 'info'}) data = [] for house in house_list: name = house.find('div', {'class': 'title'}).text.strip() price = house.find('div', {'class': 'price'}).text.strip() area = house.find('div', {'class': 'houseInfo'}).text.strip() data.append([name, price, area]) # 将数据保存到本地文件 df = pd.DataFrame(data, columns=['名称', '价格', '面积']) df.to_excel('house_data.xlsx', index=False) ``` 这是一个简单的示例代码,您可以根据您需要的数据和网站的不同来进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值