鱼C论坛

 找回密码
 立即注册
查看: 1552|回复: 6

数据爬取及打印后,每一个字符为一行,写入也是同样的问题!

[复制链接]
发表于 2020-6-20 21:58:06 | 显示全部楼层 |阅读模式

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

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

x
import requests
import bs4
import openpyxl
url = 'http://graduatedstudies.ju.edu.jo/ar/arabic/Lists/AcademicNews/School_AllNews.aspx'
res = requests.get(url)
text = bs4.BeautifulSoup(res.text, 'html.parser')
links_all = []
titles_all = []
for title in text.find_all('h4', {'class':'blog-post-title'}):
    tits = title.a.text
    links = ("http://graduatedstudies.ju.edu.jo/" + title.a['href'])
    title_all = tits + '\n ' + links
    for line in title_all:
        print(line)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-20 22:03:52 | 显示全部楼层
因为你本身  title_all 都是 字符串呀  通过 for 循环 肯定是 一个个迭代打印出来的

  1. import requests
  2. import bs4
  3. import openpyxl
  4. url = 'http://graduatedstudies.ju.edu.jo/ar/arabic/Lists/AcademicNews/School_AllNews.aspx'
  5. res = requests.get(url)
  6. text = bs4.BeautifulSoup(res.text, 'html.parser')
  7. links_all = []
  8. titles_all = []
  9. for title in text.find_all('h4', {'class':'blog-post-title'}):
  10.     tits = title.a.text
  11.     links = ("http://graduatedstudies.ju.edu.jo/" + title.a['href'])
  12.     title_all = tits + '\n ' + links
  13.     for line in title_all:
  14.         print(line,end='')
  15.     print()
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-20 22:28:11 | 显示全部楼层
Twilight6 发表于 2020-6-20 22:03
因为你本身  title_all 都是 字符串呀  通过 for 循环 肯定是 一个个迭代打印出来的

那请问,我如何将这些爬取到的数据按行保存呀,我试了一下,写入都有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-20 22:32:54 | 显示全部楼层
liminghu 发表于 2020-6-20 22:28
那请问,我如何将这些爬取到的数据按行保存呀,我试了一下,写入都有问题

你是想保存这个url 数据是嘛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-20 22:35:59 | 显示全部楼层
liminghu 发表于 2020-6-20 22:28
那请问,我如何将这些爬取到的数据按行保存呀,我试了一下,写入都有问题

  1. import requests
  2. import bs4
  3. import openpyxl
  4. url = 'http://graduatedstudies.ju.edu.jo/ar/arabic/Lists/AcademicNews/School_AllNews.aspx'
  5. res = requests.get(url)
  6. text = bs4.BeautifulSoup(res.text, 'html.parser')
  7. links_all = []
  8. titles_all = []
  9. for title in text.find_all('h4', {'class':'blog-post-title'}):
  10.     tits = title.a.text
  11.     print(tits)
  12.     links = ("http://graduatedstudies.ju.edu.jo/" + title.a['href'])
  13.     print(links)
  14.     title_all = tits + '\n ' + links
  15.     print(title_all)
复制代码

写到这一步就够了 没必要在套个 for 循环
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-21 01:33:54 | 显示全部楼层
Twilight6 发表于 2020-6-20 22:32
你是想保存这个url 数据是嘛

是的,所有打印出来的URL,和那些通知的标题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-21 02:32:08 | 显示全部楼层
Twilight6 发表于 2020-6-20 22:35
写到这一步就够了 没必要在套个 for 循环

#我现在保存数据的时候它就保存最后一天,我想保存至少前三条吧import requests
import bs4
import openpyxl
url = 'http://graduatedstudies.ju.edu.jo/ar/arabic/Lists/AcademicNews/School_AllNews.aspx'
res = requests.get(url)
text = bs4.BeautifulSoup(res.text, 'html.parser')

for title in text.find_all('h4', {'class':'blog-post-title'}):
    links = ("http://graduatedstudies.ju.edu.jo/" + title.a['href'])
    tits = title.a.text
    title_all = links, tits + '\n'
    print(title_all)
    with open('约旦大学最新公告.txt', 'w', encoding='utf-16')as file:
        file.write(str(title_all))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-3 19:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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