大lao们,我想把我爬取的数据放到myasql数据库里,该添点什么?
import requests # 发送网络请求的工具包
from lxml import etree # 转换数据类型
from pymysql import *
headers = {
# 模拟浏览器
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
}
# 设置多个网页,预留更改位置
url = 'https://www.autohome.com.cn/grade/carhtml/%d.html'
# 设置A~Z的选项网页
for i in range(ord("A"), ord("Z") + 1):
if chr(i) == 'U':
continue
new_url = f"https://www.autohome.com.cn/grade/carhtml/{chr(i)}.html" # 字符串拼接A-Z字母
# new_url = format(url%i)
respone = requests.get(url=new_url, headers=headers) # 发送请求
# print(respone.text)
# 提取我们想要的数据
html_data = etree.HTML(respone.text)
# print(html_data)
new_url_list = html_data.xpath('//ul[@class="rank-list-ul"]/li/div/a[@class="red"]/@href')
# print(new_url_list)
for new_url in new_url_list:
# 加上“https:”,使网址可以正常点击
#print("https:" + new_url)
res = requests.get("https:" + new_url)
# print(res.text)
html = etree.HTML(res.text)
car_name = html.xpath('//div[@class="athm-title"]/div/a/text()')
#print(car_name)
car_info_list = html.xpath('//div[@class="spec-wrap active"]/dl/dd')
for car_info in car_info_list:
try:
car_kind = car_info.xpath(
'.//div[@class="spec-wrap active"]/dl/dd//div[@class="name-param"]/p/a/text()')
#print(car_kind)
car_config = car_info.xpath(
'.//div[@class="spec-wrap active"]/dl/dd//div[@class="name-param"]/p/span/text()')
# print(car_config)
price = car_info.xpath('.//div[@class="guidance-price"]//span/text()')
#print(price)
except:
print("当前汽车已停产。")