鱼C论坛

 找回密码
 立即注册
查看: 1397|回复: 1

[作品展示] 抓取香港商报数据,并上传网站更新数据

[复制链接]
发表于 2020-9-18 16:22:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本来可以用dict的,但是只有两个模块,不想拆腾了。
  1. #香港商报。
  2. #by yh6788
  3. #coding:utf-8
  4. import yh_fanyi,yh_dl
  5. import time,datetime,os,requests,re
  6. from bs4 import BeautifulSoup
  7. import re
  8. import hqbsh_post
  9. import chardet
  10. import html


  11. requests.packages.urllib3.disable_warnings()

  12. def save(html):
  13.         name="1.html"
  14.         name='f:\\1.txt'
  15.         # print(html)
  16.         # html1=html.content
  17.         html1=str(html)
  18.         # print(html1)
  19.         with open(name,"w",encoding='utf8') as f:
  20.         # with open(name,"wb") as f:
  21.                 f.write(html1)
  22.         with open(name,'r',encoding='gb18030') as ff:
  23.                 txt=ff.read()
  24.         return txt

  25. def save_txts(txts,file='f:\\1.txt'):
  26.     # file='f:\\1.txt'
  27.     file1=file.replace('\\\\','\\')
  28.     # os.system('del /q %s'%file1)
  29.     os.system('copy NUL %s'%file1)
  30.     # os.system('echo ..>>%s'%file1)
  31.     # os.system('echo .>>%s'%file1)
  32.     for i in range(len(txts)):
  33.         # print(txts[i])
  34.         
  35.         with open(file,'a',encoding='gb18030') as f:
  36.             f.write(txts[i]+'\n')
  37.     return file1

  38. def post(top_txt,txt,post_0='54'):
  39.         hqbsh_username='2'
  40.         hqbsh_password='2'
  41.         fid=post_0
  42.         typeid=''
  43.         file_dir=''
  44.         hqbsh_post.main(hqbsh_username,hqbsh_password,fid,typeid,file_dir,top_txt,txt)
  45. def get_html_2(url,headers,url_0='',post_0='54'):
  46.         pass
  47.         # url='http://www.hkcd.com/content/2020-07/14/content_1202007.html'
  48.         print('\n准备进入:',url)
  49.         html_21=requests.get(url,headers=headers,verify=False)
  50.         html_21.encoding='utf8'
  51.         html_22=BeautifulSoup(html_21.text,'lxml')
  52.         html_23=html_22.find(class_='con_box')
  53.         title=html_23.find_all('h2')[0].text   #得到文件标题
  54.         # title.encoding='utf-8'
  55.         # title=yh_fanyi.fanyi_txt(title)
  56.         print('抬头:',title)
  57.         # save(title)
  58.         html_24=html_23.find(class_='content_main')
  59.         # os.exit()
  60.         txts=[]
  61.         txt=''
  62.         # html_24=html_23
  63.         # print(len(html_24))
  64.         html_24=str(html_24).split('</p>')
  65.         # print(html_24)
  66.         # print(html_24[0])
  67.         # os.exit()
  68.         for i in html_24:
  69.                 i2=[]
  70.                 # print(i)
  71.                 # time.sleep(1)
  72.                 if i=='':
  73.                         continue
  74.                 i=i+'</p>'
  75.                 i1=BeautifulSoup(i,'lxml')
  76.                 # print(i1.text)
  77.                 if '<img' in str(i):
  78.                         i2=i1.find_all('img')
  79.                 # re1=r'data-src=(.*)" alt'
  80.                 # i2=re.findall(re1,i)
  81.                 # txt_21=yh_fanyi.fanyi_txt(str(i1.text))
  82.                         if i2!=[]:
  83.                                 # print(i2)
  84.                                 for ii in i2:
  85.                                         a1=ii.get('src')
  86.                                         if 'http' in a1:
  87.                                                 txt=txt+'[img]'+a1+'[/img]'+'\n'
  88.                                                 print(a1)
  89.                                         else:
  90.                                                 txt=txt+'[img]'+url_0+a1+'[/img]'+'\n'
  91.                                                 print(url_0+a1)

  92.                                         continue

  93.                
  94.                 txt_21=i1.text
  95.                 # print(txt_21)
  96.                 if '香港商報' in txt_21:
  97.                         re2=r'(【香港.*報道:)'
  98.                         txt_22=re.findall(re2,txt_21)
  99.                
  100.                         # print(txt_22)
  101.                         if txt_22!=[]:
  102.                                 txt_21=txt_21.replace(txt_22[0],'')
  103.                 # if '【香港商報網訊】' in txt_21:
  104.                 txt_21=txt_21.replace('【香港商報網訊】','')
  105.                 txt_21=txt_21.replace('【香港商报网讯】','')
  106.                 txt_21=txt_21.replace('香港商报','')


  107.                 txt=txt+txt_21+'\n'
  108.                 # time.sleep(1)
  109.         # txt=save(txt)
  110.         print('\n\n'+txt)
  111.         kill_txt=['暴亂','黑暴','港獨','五大訴求','國安法','香港自治法案']
  112.         for i3 in kill_txt:
  113.                 if i3 in title+txt:
  114.                         print('%s在txt里面,该条目不上传。'%i3)
  115.                         print('\a')
  116.                         time.sleep(5)
  117.                         return ''
  118.         # os.exit()

  119.         # txt.replace('ü','')
  120.         txt=txt.replace('http://','https://')
  121.         post(title,txt,post_0)

  122. def get_html_1(url,headers,post_0='54'):
  123.         html_1=requests.get(url,headers=headers,verify=False)
  124.         html_1.encoding='utf8'
  125.         html_2=BeautifulSoup(html_1.text,'lxml')
  126.         html_3=html_2.find(class_="imps_new")
  127.         html_3=html_3.find(class_="cat_new_zs cat_newlist")
  128.         html_4=html_3.find_all('ul')
  129.         print(len(html_4[0]))
  130.         # html_4=html_4.decode('utf-8')
  131.         # print(html_4[0])

  132.         yesterday = (datetime.datetime.now() + datetime.timedelta(days=-1)).date().strftime('%Y-%m-%d')
  133.         print(yesterday)

  134.         # yesterday='2020-07-14'
  135.         # print(yesterday)
  136.         # os.exit()
  137.         url_0='http://www.hkcd.com/'
  138.         urls=[]
  139.         n=0
  140.         for i in html_4[0]:
  141.                 i1=[]
  142.                 # print(i)
  143.                 if yesterday in str(i):
  144.                         # print(i)
  145.                         # print('============')
  146.                         i1=i.find_all('a')
  147.                         if i1!=[]:
  148.                                 # print(i1[0])
  149.                                 title=i1[0].text
  150.                                 re_1=r'href="(.*)" target='
  151.                                 url1=re.findall(re_1,str(i1[0]))
  152.                                 url=url_0+url1[0]
  153.                                 urls.append(url)
  154.                                 # print(url)
  155.                                 get_html_2(url,headers,url_0,post_0)
  156.                                 n+=1
  157.                                 # os.exit()
  158.                                 # print('\a')
  159.                                 # print('-------------')
  160.                                 # time.sleep(1)
  161.         print(n,'个数据已经处理。')

  162. def main():
  163.         user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
  164.         headers={"User-Agent":user_agent}

  165.         #人在香港
  166.         url='http://www.hkcd.com/node_61.html'
  167.         post_0='54'
  168.         #民用科技
  169.         url_1='http://www.hkcd.com/node_24.html'
  170.         post_1='46'

  171.         os.system('cls')
  172.         get_html_1(url,headers,post_0)
  173.         get_html_1(url_1,headers,post_1)


  174. if __name__=='__main__':
  175.         yh_dl.kill_system_dl()
  176.         main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-12-23 15:03:26 From FishC Mobile | 显示全部楼层
你那个妹子图代码出问题了,你能更新更新吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-8 00:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表