Python 爬取国家级非物质文化遗产代表性项目名录代码(https://www.ihchina.cn/)

//(第三方库可以用清华的镜像网站安装:Simple Index)
import json
import requests
import pandas as pd

title = []
type1 = []
unit = []
content = []
protect_unit = []
rx_time = []

for i in range(1,307):
    print('爬取第{}页'.format(i))
    recommed_url = 'http://www.ihchina.cn/Article/Index/getProject.html?province=&rx_time=&type=&cate=&keywords=&category_id=16&limit=10&p={0}'.format(i)

    try:
        response = requests.get(url=recommed_url)
        response.raise_for_status()  # 如果请求不成功,会抛出异常
        diss_dict = response.json()

        for item in diss_dict['list']:
            title.append(item['title'])  # 名称
            type1.append(item['type'])  # 类型
            unit.append(item['unit'])  # 申报地区或单位
            protect_unit.append(item['protect_unit'])  # 保护单位
            content.append(item['content'])  # 内容
            rx_time.append(item['rx_time'])  # 时间
    except requests.exceptions.RequestException as e:
        print(f"请求第 {i} 页时出现问题: {e}")

data = {
    '名称': title,
    '类型': type1,
    '申报地区或单位': unit,
    '保护单位': protect_unit,
    '内容': content,
    '时间': rx_time
}

df = pd.DataFrame(data)

try:
    with pd.ExcelWriter('国家级非物质文化遗产.xlsx', engine='xlsxwriter') as writer:
        df.to_excel(writer, index=False, sheet_name='Sheet1')
    print("数据已成功保存到 Excel 文件")
except Exception as e:
    print(f"保存到 Excel 文件时出现问题: {e}")

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要了解得物网站的数据结构和方式,得物网站比较复杂,需要使用Selenium+BeautifulSoup进行。 以下是一个简单的得物Python代码实现(注:仅供学习参考,请勿用于商业用途): ```python import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from bs4 import BeautifulSoup options = Options() options.add_argument('--no-sandbox') # 解决DevToolsActivePort文件不存在报错的问题 options.add_argument('window-size=1920x3000') # 指定浏览器分辨率 options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面 options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度 options.add_argument('--headless') # 无界面 driver = webdriver.Chrome(options=options) url = 'https://www.dewu.com/' driver.get(url) # 等待页面加载完成 time.sleep(3) # 模拟鼠标点击,展开商品列表 driver.find_element_by_xpath('//div[text()="全部商品"]').click() # 等待页面加载完成 time.sleep(3) # 获页面源代码 html = driver.page_source # 解析页面 soup = BeautifulSoup(html, 'html.parser') # 获商品列表 items = soup.find_all('div', {'class': 'item-card'}) for item in items: # 获商品标题 title = item.find('div', {'class': 'title'}).text.strip() # 获商品价格 price = item.find('div', {'class': 'price'}).text.strip() # 获商品链接 link = item.find('a', {'class': 'item-link'})['href'] print(title, price, link) # 关闭浏览器 driver.quit() ``` 这里的代码仅仅是一个简单的虫示例,如果想要更加深入地了解得物网站的数据结构和方式,需要结合具体的需求进行更加详细的分析和实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值