鱼C论坛

 找回密码
 立即注册
查看: 1821|回复: 0

[技术交流] 外国的爬虫

[复制链接]
发表于 2021-9-7 17:58:54 | 显示全部楼层 |阅读模式

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

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

x
  1. # -*- coding: utf-8 -*-

  2. import requests
  3. from bs4 import BeautifulSoup
  4. from fake_useragent import UserAgent
  5. import re
  6. from openpyxl import Workbook
  7. import time

  8. wb = Workbook()
  9. ws = wb.active
  10. ws.title = "Sheet1"
  11. #   获取采集的条码
  12. csv_str = open('book0906.csv','r')
  13. #   按行读取
  14. list_d = csv_str.readlines()
  15. #   代理地址
  16. proxies = {
  17.     'https':'http://127.0.0.1:1080'
  18. }
  19. user_agent = UserAgent(use_cache_server=False)
  20. #   获取内容网站的url地址
  21. def url(self):
  22.     headers = {
  23.                     "user-agent": user_agent.random
  24.                 }
  25.     html = requests.get(self,proxies=proxies,headers=headers).text
  26.     bs4 = BeautifulSoup(html,'html.parser')
  27.     html_bs4 = bs4.find(class_='resultBooksImg resultBooksImg')
  28.     urld = 'https://www.sanmin.com.tw/'+re.sub('href=|"','',re.findall('href=".*?"',str(html_bs4))[0])
  29.     return urld
  30. class book():
  31.     #   获取书籍内容
  32.     def book_lr(self):
  33.         book_lrs =[]
  34.         ddd = self.find(class_='bookProject tabsview')
  35.         for d in ddd.find_all(class_='productContent'):
  36.             book_lrs.append(d.text)
  37.         return book_lrs
  38.     #   书籍信息
  39.     def book_xx(self):
  40.         book_xx =[]
  41.         for d in self.find_all(class_='mainText ga'):
  42.             book_xx.append(d.text)
  43.         return book_xx
  44.     def book_sx(self):
  45.         for i in bookxx:
  46.             if  len(re.findall(self,i)) == 1:
  47.                 jj = re.sub(self + '|\:','',i)
  48.                 return jj
  49.             else:
  50.                 pass

  51. for str_list_num in list_d:
  52.     try:
  53.         books = []
  54.         headers = {
  55.                         "user-agent": user_agent.random
  56.                     }
  57.         str_list = str_list_num.strip('\n')
  58.         url1 = url('https://www.sanmin.com.tw/search/index/?ct=ISBN&qu='+str_list+'&ls=SD')
  59.         html = requests.get(url1,proxies=proxies,headers=headers).text
  60.         bs4 = BeautifulSoup(html,'html.parser')
  61.         book_name = bs4.find(class_='bookStatusInfor')
  62.         bookxx = book.book_xx(book_name)
  63.         books.append(str_list)
  64.         books.append(book_name.find('li').text)
  65.         books.append(book.book_sx('作者'))
  66.         mony = re.findall('[0-9].*[0-9]',re.findall('定.*.?元',bs4.find(class_='bookStatusAddCtrl').text)[0])
  67.         books.append(mony[0])
  68.         books.append(book.book_sx('規格'))
  69.         books.append(re.findall('[0-9].*[0-9]',book.book_sx('裝訂/頁數'))[0])
  70.         books.append(re.sub('/','',re.findall('.*/',book.book_sx('裝訂/頁數'))[0]))
  71.         books.append(book.book_sx('出版日'))
  72.         kc = re.findall('預購中.*|庫存.*',bs4.find(class_='bookStatusAddCtrl').text)[0]
  73.         books.append(kc)
  74.         books.append(book.book_sx('圖書分類'))
  75.         books.append(book.book_lr(bs4)[0])
  76.         try:
  77.             books.append(book.book_lr(bs4)[1])
  78.         except:
  79.             books.append('')
  80.         try:
  81.             books.append(book.book_lr(bs4)[3])
  82.         except:
  83.             books.append('')
  84.         ws.append(books)
  85.         wb.save('sanmin.xlsx')

  86.     except:
  87.         pass
  88. wb.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 22:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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