获取城市名称
存在一个没有解决的问题:用csv读写每个词都被逗号分开,实在没有办法了,用的普通的读写文件
第一个确实可以,但是我不能很好的把第二个中的正则表达式用进去,所以只能这样了
#获取城市名称1
import re
import csv
import time
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
def get_url():
url = 'http://www.maps7.com/china_province.php' # http://www.maps7.com/china_province.php
return url
def get_urlText(url):
try:
kv = {'user-agent': 'Mozilla/5.0'}
r = requests.get(url, headers=kv)
r.raise_for_status()
r.encoding = r.apparent_encoding # 使其encoding更准确
# print (r.text) #1
return r.text
except:
print('error 1')
return
def get_parseText(parse_url):
try:
city_list = []
soup = BeautifulSoup(parse_url, 'html.parser')
for elem in soup.find_all('a'):
city_list.append(elem.text)
return city_list
except:
print("error 2")
return
url = get_url()
parse_text = get_urlText(url)
cityList = get_parseText(parse_text)
with open('cityname.csv', 'w', newline='\n', encoding='utf8') as f:
f = csv.writer(f)
for row in cityList:
f.writerow(row)
#获取城市名称2
def get_urlText(url):
try:
kv = {'user-agent': 'Mozilla/5.0'}
r = requests.get(url, headers=kv)
r.raise_for_status()
r.encoding = r.apparent_encoding # 使其encoding更准确
# print (r.text) #1
return r.text
except:
print('error 1')
return
def get_lists(parse_url):
try:
province= ''
city=''
lists=[]
for num in range(0, 34):
if (num == 33):
text = re.findall('<a name="33"(.*)</a>',parse_url)
else:
text = re.findall('<a name="' + str(num) + '" href=.*?>(.*?)<a name="' + str(num + 1) + '" href=.*?>',parse_url)
for i in text:
if (num == 0): #北京
province = re.findall('(.*?)</a></h4>', i)
else:
province = re.findall('<h4>(.*?)</h4>', i)
city = re.findall('<a href="/china/dianziditu.*?>(.*?)</a>', i)
for i in province:
if (len(city) != 0):
for j in city:
lists.append(i+''+j)
else:
lists.append(i+' '+i)
return lists
except:
print("error 2")
return
url = 'http://www.maps7.com/china_province.php'
parse_text = get_urlText(url)
cityList=get_lists(parse_text)
f=open('cityname.txt','w',encoding='utf-8')
for elem in cityList:
f.write(elem)
f.write('\r')
f.close()