python代码壁纸-一个爬取壁纸的python代码

importrequestsfrom lxml importetreeimportosclassBizi(object):def __init__(self):#如果爬到一半就停止了,想从某页开始爬取就改一些下index_后面的数字改为你想爬的页码就可以了

self.url = 'http://www.netbian.com/dongman/index_2.htm'self.headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}#获取网页数据

defGet_data(self,url):#print(url)

req = requests.get(url=url, headers=self.headers)return req.content.decode('GBK')#获取图片名字、图片页面链接、文件夹页码、下一页链接

defLink_name(self,data):

html=etree.HTML(data)

list_name= html.xpath('//*[@id="main"]/div[2]/ul/li/a/b/text()')

list_link= html.xpath('//*[@id="main"]/div[2]/ul/li/a/@href')

wname= html.xpath('//*[@id="main"]/div[3]/b/text()')

net_url1= html.xpath('//*[@id="main"]/div[3]/a[10]/@href')returnlist_name,list_link,wname,net_url1#拿取图片的链接

defTu_link(self,t_link):

req2= requests.get(url=t_link)

html2=etree.HTML(req2.text)

jpg_link= html2.xpath('//*[@id="main"]/div[2]/div/p/a/img/@src')returnjpg_link#获取图片

defTu_data(self,tu_bin):

req3= requests.get(url=tu_bin)returnreq3.contentdefrun(self):

net_url=self.url

ab= '2'

whileTrue:try:

index_data=self.Get_data(net_url)

name,link,fname,net_url=self.Link_name(index_data)#print(net_url)

net_url = 'http://www.netbian.com' +str(net_url[0])print(f"正在爬取第{ab}页")

ab= int(fname[0]) + 1

#print(name,link)

#字典化

dis =dict(zip(name,link))#循环拿到key,然后就是可以利用key拿到value

for i indis:

lb_link= self.Tu_link('http://www.netbian.com'+str(dis[i]))## print(str(lb_link[0]))

tu_bindata =self.Tu_data(str(lb_link[0]))try:

os.mkdir('第'+str(fname[0])+'页')

f= open('./第' + str(fname[0]) + '页/' + str(i) + '.jpg', 'wb')

f.write(tu_bindata)

f.close()exceptException as e:#print(e)

f = open('./第'+str(fname[0])+'页/'+str(i)+'.jpg', 'wb')

f.write(tu_bindata)

f.close()exceptException as l:#print(l)

print('这页没了哦')#break

if __name__ == '__main__':

bz=Bizi()

bz.run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下如何使用 Python 爬虫来爬取房产数据并保存到本地。 首先,您需要选择一个合适的网站来进行爬取。例如,可以选择链家网、安居客等房产网站。然后,您需要分析该网站的页面结构和数据请求方式,确定需要爬取哪些数据,以及如何获取这些数据。 接着,您可以使用 Pythonrequests 库来发送 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、付费专栏及课程。

余额充值