import pymysql
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
db = pymysql.connect(host='1', user='1', password='1', database='1')
cursor = db.cursor()
def insert_data(province, city, url, db, cursor):
try:
sql = 'insert into baixingwang_info(`province`,`city`,`url`)values (%s,%s,%s)'
cursor.execute(sql, (province, city, url))
db.commit()
except Exception as e:
print(e)
def start():
chrome_options = Options()
browser = webdriver.Chrome(options=chrome_options)
browser.get('https://www.baixing.com/?changeLocation=yes')
browser.set_page_load_timeout(15)
browser.set_script_timeout(15)
zhixiashi(browser)
all_city(browser)
browser.quit()
def all_city(browser):
province = browser.find_elements_by_xpath('//ul[@class="wrapper"]//li//div[@class="city-sec"]')
for i in province:
province_1 = i.find_element_by_xpath('.//h5//a').text
city_3 = i.find_elements_by_xpath('.//ul[@class="collapsed"]//li/a[position()=1]')
for j in city_3:
city = j.text
link = j.get_attribute("href")
insert_data(province=province_1, city=city, url=link, db=db, cursor=cursor)
def zhixiashi(browser):
zxs = browser.find_elements_by_xpath('//ul[@class="wrapper"]//li[2]')
for i in zxs:
zxs_city = i.find_elements_by_xpath('.//ul//li/a[position()=1]')
for j in zxs_city:
city = j.text
link = j.get_attribute("href")
province_1 = j.text
insert_data(province=province_1, city=city, url=link, db=db, cursor=cursor)
if __name__ == '__main__':
start()
百姓网城市--省,市,爬取
最新推荐文章于 2024-06-21 20:37:46 发布