1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| import numpy as np import requests,re from bs4 import BeautifulSoup
def names(url): a = [] b=[] r = requests.get(url,timeout=30, headers=myHeader) r.raise_for_status() r.encoding = 'utf-8' html = r.text soup = BeautifulSoup(html, "html.parser") dt = soup.find_all('a', attrs={'class': 'journal-item cf'}) for i in dt: a.append('https://you.ctrip.com'+i.get('href')) for i in a: if 'nanjing' in i: b.append(i) return b
def spans(i,url,count): try: a=[] r = requests.get(url, headers=myHeader) r.raise_for_status() r.encoding = 'utf-8' html = r.text soup = BeautifulSoup(html, "html.parser") div=soup.find('div',attrs={'class':'ctd_content_controls cf'}) span1=div.find_all('span') for i in span1: a.append(re.sub(u"\\<.*?\\>",'',str(i))) return a except Exception as ex: a='' print("第{}页{}行出错:{}".format(i,count,ex)) return a
def values(span): result = [] result1 ='无 ' result2 =result4= ' 无 ' result3 = ' 无 ' result5 = ' ' for i in range(len(span)): i = re.sub(u"\\<.*?\\>", '', span[i]).replace(' ', '') if i[0:3] == '天数:': result1 = i[3:] if i[0:3] == '时间:': result2 = i[3:] if i[0:3] == '人均:': result3 = i[3:] if i[0:3] == '和谁:': result4 = i[3:] if i[0:3] == '玩法:': result5 = i[3:].replace('玩法:', '') result.append(result1) result.append(result2) result.append(result3) result.append(result4) result.append(result5) return result
def dls(url): try: a = '' r = requests.get(url, headers=myHeader) r.raise_for_status() r.encoding = 'utf-8' html = r.text soup = BeautifulSoup(html, "html.parser") dl1 = soup.find('div', attrs={'class': 'author_poi'}) dd=dl1.find_all('dd') for i in dd: a1=i.find('a') a=a+a1.string+' ' return a except Exception as ex: a=' ' return a
def money(all_value): a=0 j=0 for i in all_value: if i[2]!=' 无 ': a=a+int(str(i[2]).replace('元','')) j+=1 return a/j
def month(all_value): a=[0,0,0,0,0,0,0,0,0,0,0,0] s=[] for i in all_value: if i[1]!=' 无 ': a[int(str(i[1]).replace('月',''))-1]+=1 for i in range(len(a)): if a[i]==np.max(a): s.append(str(i+1)+'月') return s
all_value=[] myHeader = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'} for i in range(1,5): with open("南京游记.txt", 'a+', encoding='utf-8') as file: file.writelines('第{}页'.format(i)) file.write("\n") url = "https://you.ctrip.com/travels/nanjing9/t3-p{}.html".format(i) name=names(url) count = 0 for j in name: count+=1 span=spans(i,j,count) if span!='': value=values(span) dl=dls(j) value.append(dl) with open("南京游记.txt",'a+',encoding='utf-8') as file: for k in value: file.write(k+' ') file.write('\n') all_value.append(value) print(f'第{i}页打印成功') print('平均花费',money(all_value)) print("推荐月份",month(all_value))
|