简体   繁体   English

Python 美丽汤 - output.string 结果分别

[英]Python Beautiful Soup - output .string results separately

I am creating a discord bot that scrapes book titles from a website and I am looking to differentiate the different results I am getting.我正在创建一个 discord 机器人,它可以从网站上抓取书名,我希望区分我得到的不同结果。

name = "Raymond"
r = requests.get("https://websitehere/s/{}?order=bestmatch".format(name), headers=headers, verify=False)
results = r.text
soup = BeautifulSoup(results, "lxml")
for a in soup.find_all("a",style="text-decoration: underline;", limit=5):

With this code I am retrieving the first 5 titles of the search result.使用此代码,我正在检索搜索结果的前 5 个标题。 Since it is for a discord bot I need to output them one by one to put them in the following expression:由于它是针对 discord 机器人的,因此我需要将 output 一个一个地放入以下表达式中:

message = f"{a.string[1]}\n\n"
message += f"{a.string[2]}\n"
message += f"{a.string[3]}\n"
message += f"{a.string[4]}\n"
message += f"{a.string[5]}\n"

if I use a.string it will give me the 5 results at the same time and a.string[1] outputs only 1 letter (which isn't even the first letter of the title...)如果我使用 a.string 它会同时给我 5 个结果,并且 a.string[1] 只输出 1 个字母(甚至不是标题的第一个字母......)

Which argument could I use?我可以使用哪个论点?

EDIT: minimal code --编辑:最少的代码——

import requests, urllib3
from bs4 import BeautifulSoup
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

name = "Raymond"
r = requests.get("https://1lib.ch/s/{}?order=bestmatch".format(name), verify=False)
results = r.text
soup = BeautifulSoup(results, "lxml")
for a in soup.find_all("a",style="text-decoration: underline;", limit=2):
    print(a.string)

Output: Output:

Raymond Carver
Technique and sensibility in the fiction and poetry of Raymond Carver

What I'd like to do is to have a result as follows and this automatically.我想做的是自动获得如下结果。

message = f"{"Raymond Carver"}\n\n"
message += f"{"Technique and sensibility in the fiction and poetry of Raymond Carver"}\n"

Thanks for your help.谢谢你的帮助。

Gather the tag contents first as a list then join them together later.首先将标签内容收集为列表,然后将它们连接在一起。

a_strings = [a.string for a in for a in soup.find_all("a",style="text-decoration: underline;", limit=5)]

message = '\n'.join(a_strings)

If you want an extra linebreak after the first line, then you can insert an empty string at index 1 before you do the join.如果您想在第一行之后有一个额外的换行符,那么您可以在执行连接之前在索引 1 处插入一个空字符串。

a_strings.insert(1, '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM