python实现去掉https://ipchaxun.com/conn-push.jk.cn/的https://ipchaxun.com/和最后的/
时间: 2023-06-23 15:06:15 浏览: 146
可以使用Python的字符串替换功能来实现去掉"https://ipchaxun.com/"和最后的"/",具体代码如下:
```
url = "https://ipchaxun.com/conn-push.jk.cn/"
new_url = url.replace("https://ipchaxun.com/", "").rstrip("/")
print(new_url)
```
这段代码中,我们先定义了url变量为"https://ipchaxun.com/conn-push.jk.cn/",然后使用replace函数将"https://ipchaxun.com/"替换成空字符串,得到的结果是"/conn-push.jk.cn/",最后使用rstrip函数去掉末尾的"/"。最终输出的结果是"conn-push.jk.cn"。
相关问题
用python实现scrapy框架的爬虫,将数据存在桌面的csv格式并存入mysql数据库。 初始页面为https://lishi.tianqi.com/beijing/index.html, 点击“选择月份”为8、9、10后分别跳转到https://lishi.tianqi.com/beijing/202208.html,https://lishi.tianqi.com/beijing/202209.html,https://lishi.tianqi.com/beijing/202210.html, 在scrapy中实现自动翻页效果
好的,这是一个比较完整的爬虫项目,需要考虑很多方面。下面是一个大致的思路:
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 数据库中。
以上就是一个大致的思路,具体的实现细节还需要根据实际情况进行调整。
利用python爬取https://interface.sina.cn/news/wap/fymap2020_data.d.json的数据并放入数据库
好的,首先我们需要用到 `requests` 和 `pymysql` 这两个库。请确保你已经安装了这些库。
首先,我们需要从给出的链接中获取 JSON 数据。可以使用以下代码:
```python
import requests
url = 'https://interface.sina.cn/news/wap/fymap2020_data.d.json'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(data)
```
接下来,我们需要将数据存储到数据库中,可以使用以下代码实现:
```python
import pymysql
# 连接数据库
conn = pymysql.connect(
host='localhost',
user='root',
password='123456',
database='covid19',
charset='utf8mb4'
)
# 获取游标
cursor = conn.cursor()
# 创建数据表
create_table_sql = '''
CREATE TABLE IF NOT EXISTS covid19 (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
confirmed INT NOT NULL,
cured INT NOT NULL,
dead INT NOT NULL,
continent VARCHAR(20) NOT NULL,
countryType INT NOT NULL,
createTime BIGINT NOT NULL,
modifyTime BIGINT NOT NULL,
tags VARCHAR(50) NOT NULL
)
'''
cursor.execute(create_table_sql)
# 插入数据
insert_sql = '''
INSERT INTO covid19 (name, confirmed, cured, dead, continent, countryType, createTime, modifyTime, tags)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
'''
for item in data['data']['list']:
cursor.execute(insert_sql, (item['name'], item['confirmed'], item['cured'], item['dead'], item['continent'],
item['countryType'], item['createTime'], item['modifyTime'], item['tags']))
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()
```
这样就完成了,代码中的 `covid19` 数据库需要提前创建好,同时也需要根据需要修改数据库连接信息。