数据爬取及打印后,每一个字符为一行,写入也是同样的问题!
import requestsimport 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) 因为你本身title_all 都是 字符串呀通过 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')
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,end='')
print()
Twilight6 发表于 2020-6-20 22:03
因为你本身title_all 都是 字符串呀通过 for 循环 肯定是 一个个迭代打印出来的
那请问,我如何将这些爬取到的数据按行保存呀,我试了一下,写入都有问题 liminghu 发表于 2020-6-20 22:28
那请问,我如何将这些爬取到的数据按行保存呀,我试了一下,写入都有问题
你是想保存这个url 数据是嘛 liminghu 发表于 2020-6-20 22:28
那请问,我如何将这些爬取到的数据按行保存呀,我试了一下,写入都有问题
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
print(tits)
links = ("http://graduatedstudies.ju.edu.jo/" + title.a['href'])
print(links)
title_all = tits + '\n ' + links
print(title_all)
写到这一步就够了 没必要在套个 for 循环 Twilight6 发表于 2020-6-20 22:32
你是想保存这个url 数据是嘛
是的,所有打印出来的URL,和那些通知的标题 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))
页:
[1]