Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有更好的方法来区分书名和作者?_Python_Split_Beautifulsoup - Fatal编程技术网

Python 有没有更好的方法来区分书名和作者?

Python 有没有更好的方法来区分书名和作者?,python,split,beautifulsoup,Python,Split,Beautifulsoup,这是划分标题和作者的最佳方式吗 我需要将书名和作者从一个文本块中分割出来,并将其放入一个元组列表中,但我发现很难对其进行理解。这是文本块的一个示例: The Coddling of the American Mind: How Good Intentions and Bad Ideas Are Setting Up a Generation for Failure by Greg Lukianoff & Jonathan Haidt The Four Agreements: A Pra

这是划分标题和作者的最佳方式吗

我需要将书名和作者从一个文本块中分割出来,并将其放入一个元组列表中,但我发现很难对其进行理解。这是文本块的一个示例:

The Coddling of the American Mind: How Good Intentions and Bad Ideas Are Setting Up a Generation for Failure
by Greg Lukianoff & Jonathan Haidt

The Four Agreements: A Practical Guide to Personal Freedom (A Toltec Wisdom Book)
by Don Miguel Ruiz
这是一个使用BeautifulSoup从html中提取标题和作者的Python脚本

result = url_connection(url_list[3]) #prints the first link for testing
x = result.find_all("h3", {"class" : "book-title"})
for a in x:
    list_of_books.append(tuple((a.text).replace('\n', '').split('by')))
我得到的结果是正确的:

[('The Coddling of the American Mind: How Good Intentions and Bad Ideas Are Setting Up a Generation for Failure', 'Greg Lukianoff & Jonathan Haidt'), ('The Four Agreements: A Practical Guide to Personal Freedom (A Toltec Wisdom Book), 'Don Miguel Ruiz')]
然而,如果标题中有一个by,那么我的代码就完了。执行此操作的最佳方法是什么?

使用rsplit'by',1代替“按”拆分

这将从字符串的末尾开始查找,并在一次拆分后停止


我在前面加了一个空格,以防万一作者是丹·雅各比和约翰·多伊。

谢谢你,这很有魅力。但是,在“by”之前不需要空格,因为我需要它在字符串或换行符的开头查找它。在这种情况下,我们可以看到有问题的url吗?而不是在抓取字符串后操纵它,这将更容易获得标题和作者分别而刮你需要分享的url/html,以便我们可以帮助你做到这一点。在使用字符串操作时,您可能会错过某些情况。