python day3--爬取每日一句

import requests
import re
from bs4 import BeautifulSoup

url='http://wufazhuce.com/one/'#每一期的链接共同的部分
words=['0']*1800#定义一个长度为1800的列表,用来保存每一句话,并初始化为全‘0’
for i in range(0,100):
    s=str(i)#数字类型转为字符串类型
    print(i)
    currenturl=url+s#当前期的链接
    try:
        res=requests.get(currenturl)
        res.raise_for_status()
    except requests.RequestException as e:#处理异常
        print(e)
    else:
        html=res.text#页面内容
        soup = BeautifulSoup(html,'html.parser')
        a=soup.select('.one-titulo')#查找期次所在的标签
        b=soup.select('.one-cita')#查找“每日一句”所在的标签
        #print(b)
        index=re.sub("\D","",a[0].string.split()[0])#从“vol.xxx”提取期次数值作为下标
        #print(index)
        if(index==''):
            continue
        #print(b[0].string.split())
        words[int(index)]=b[0].string.split()#将该期“每日一句”存入列表
        #print(words[int(index)])
print("begin!!!!!!!!!!!!!!!")      
f=open('C:\\Users\\lsy\\Desktop\\one.TXT','w')#将每句话写入这个txt文件中,先打开
for i in range(1,1774):
    if(words[i]=='0'):
        continue
    else:
        print(words[i])
        f.writelines('VOL.'+str(i)+'\n')#写入期次和换行
        f.writelines('    ')#每句话开始空四格
        f.writelines(words[i])#写入该句话
        f.writelines('\n\n')#换行,并空一行写入下一句        
f.close()#关闭文件

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您介绍一下如何使用 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) ``` 这是一个简单的示例代码,您可以根据您需要爬取的数据和网站的不同来进行修改和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值