Python3 reptiles get in batches country code

Copyright: As this article have questions, please contact the author micro letter kxymxzs, welcome harassment! https://blog.csdn.net/MG_ApinG/article/details/87547981

Man of few words said, directly on the code. .

#coding: utf-8
import re
import urllib
from urllib import request
from urllib.parse import quote
import pandas as pd
import string
import time


ydylCountry = pd.read_csv(r'F://data_temp/ydyl.csv')
fileGetSavePath = r'F://data_temp/ydyl.txt'
country_nbr = []
for i in ydylCountry['country']:
	try:
		urlAim = 'https://www.chahaoba.com/{0}'.format(i)
		url = quote(urlAim, safe=string.printable)
		headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
		req = urllib.request.Request(url=url, headers=headers)
		response = urllib.request.urlopen(req)
		html = response.read()
		content = html.decode("utf-8")
		pattern = re.compile('<b>国际区号:</b>.*?<a href=".*?" title=".*?">(.*?)</a>', re.S|re.M)
		itemList = re.findall(pattern, content)
		dissGet = '{0}|{1}'.format(i, itemList[0])
		print(dissGet)
		with open(fileGetSavePath,'a') as f:
			f.write(dissGet)
			f.write('\n')
			f.close()
		time.sleep(2)
	except:
		dissGet = '{0}|mistake'.format(i)
		print(dissGet)
		with open(fileGetSavePath,'a') as f:
			f.write(dissGet)
			f.write('\n')
			f.close()
		time.sleep(6)

The result is this

中国|86
蒙古国|976
新加坡|65
马来西亚|60
印度尼西亚|62
缅甸|95
泰国|66
老挝|856
柬埔寨|855
越南|84
文莱|673
菲律宾|63
伊朗|98
伊拉克|964
土耳其|90
叙利亚|963
约旦|962
黎巴嫩|961
以色列|972
巴勒斯坦|930

 

Guess you like

Origin blog.csdn.net/MG_ApinG/article/details/87547981