Could not parse configuration: /hibernate.cfg.xml

hibernate需要联网验证dtd,错误原因:未联网或网速不行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较完整的爬虫项目,需要考虑很多方面。下面是一个大致的思路: 1. 创建一个 Scrapy 项目,定义好 Item 和 Spider。 2. 在 Spider 中定义初始页面和翻页的规则。首先要爬取的是 https://lishi.tianqi.com/beijing/index.html 这个页面,然后在该页面中找到“选择月份”的链接,用它来构造翻页规则,例如: ```python start_urls = ["https://lishi.tianqi.com/beijing/index.html"] def parse(self, response): # 获取“选择月份”的链接 month_links = response.css('.tqtongji2 .tqmonth a::attr(href)').getall() for link in month_links: yield scrapy.Request(url=link, callback=self.parse_month_page) ``` 在 `parse_month_page` 方法中,可以解析每个月份的页面,并把数据存入 CSV 文件和 MySQL 数据库中。 3. 在 Item 中定义需要爬取的数据字段,例如日期、温度、气压、风力等: ```python class WeatherItem(scrapy.Item): date = scrapy.Field() temperature = scrapy.Field() pressure = scrapy.Field() wind = scrapy.Field() ``` 4. 在 Spider 中解析每个月份的页面,获取数据并存入 CSV 文件和 MySQL 数据库中。首先要找到每个月份页面中的天气数据表格,然后循环解析每一行数据: ```python def parse_month_page(self, response): # 找到天气数据表格 table = response.css('.tqtongji1 table')[1] rows = table.css('tr') for row in rows[1:]: # 解析每一行数据 item = WeatherItem() item['date'] = row.css('td:nth-child(1)::text').get() item['temperature'] = row.css('td:nth-child(2)::text').get() item['pressure'] = row.css('td:nth-child(3)::text').get() item['wind'] = row.css('td:nth-child(4)::text').get() yield item ``` 5. 在 Spider 中定义数据存储管道,将数据存入 CSV 文件和 MySQL 数据库中: ```python class WeatherPipeline: def open_spider(self, spider): self.file = open('weather.csv', 'w', encoding='utf-8', newline='') self.writer = csv.writer(self.file) self.writer.writerow(['日期', '温度', '气压', '风力']) self.conn = pymysql.connect(host='localhost', user='root', password='123456', db='test', charset='utf8') self.cursor = self.conn.cursor() def close_spider(self, spider): self.file.close() self.conn.close() def process_item(self, item, spider): # 写入 CSV 文件 self.writer.writerow([item['date'], item['temperature'], item['pressure'], item['wind']]) # 存入 MySQL 数据库 sql = "INSERT INTO weather(date, temperature, pressure, wind) VALUES (%s, %s, %s, %s)" values = (item['date'], item['temperature'], item['pressure'], item['wind']) self.cursor.execute(sql, values) self.conn.commit() return item ``` 6. 在 Spider 中定义翻页的规则。根据“选择月份”的链接构造出每个月份页面的 URL,例如 https://lishi.tianqi.com/beijing/202208.html,https://lishi.tianqi.com/beijing/202209.html,https://lishi.tianqi.com/beijing/202210.html 等等,可以使用 `scrapy.Request` 方法来构造请求: ```python def parse(self, response): # 获取“选择月份”的链接 month_links = response.css('.tqtongji2 .tqmonth a::attr(href)').getall() for link in month_links: # 构造每个月份页面的 URL url = response.urljoin(link.replace('.html', '')) for i in range(1, 32): # 构造每一天的 URL day_url = url + f'{i:02d}.html' yield scrapy.Request(url=day_url, callback=self.parse_day_page) ``` 在 `parse_day_page` 方法中,可以解析每一天的页面,并把数据存入 CSV 文件和 MySQL 数据库中。 以上就是一个大致的思路,具体的实现细节还需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值