【发布时间】:2021-03-15 01:28:47
【问题描述】:
我终其一生都无法弄清楚如何获取此列表中的元素(它们本身就是列表),以便在我将它们写入文件时打印成多行。我从网站上刮掉标题,然后刮掉链接。最终目标(为了您的洞察力)是将标题和链接以如下格式配对:
<a href='www.mywebsite.com/curry-recipe'>Curry Recipe<a/>
但就目前而言,问题是虽然我最终的 desfinalList 看起来还不错,例如:
[['Curry Recipe', 'www.originalwebsite.com/curry-recipe'], ['Pancake Recipe', 'www.originalwebsite.com/pancake-recipe']]
如果不将其全部放入一大行中,我似乎无法将其打印到文件中。使用文本换行,它在视觉上是易于管理的,但我更喜欢它在多行上。
有问题的代码是最后一个块。
def OFDdesserts():
urlA = 'https://olivesfordinner.com/category/dessert/page/{}'
for i in range(2,5):
url = urlA.format(i)
response = requests.get(url)
htmlText = response.text
soup = BeautifulSoup(htmlText, 'lxml')
links = soup.find_all('article')
for title in links[0:12]:
titleActual = title.get('aria-label')
if 'Giveaway' not in titleActual:
hyperL = title.find('header', class_ = 'entry-header').a['href']
if titleActual not in desTitleList:
desTitleList.append(titleActual)
desLinkList.append(hyperL)
desList3.append([[x,y] for x,y in zip(desTitleList, desLinkList)])
#erase duplicates
for item in desList3:
if item not in desfinalList:
desfinalList.append(item)
#write the file
for elem in desfinalList:
with open('recipes/desserts.txt', 'w') as f:
f.write('\n \n'.join(map(str, desfinalList)))
print('just added something yummy to desserts!')
【问题讨论】:
-
您的代码对我来说很好用。列表示例列表创建一个文件,两个列表之间有一个空行。我在windows PC上,如果这有什么不同的话。你确定你提供的例子是你真正要处理的?
-
是的,我复制粘贴了
标签: python-3.x list web-scraping beautifulsoup file-writing