首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从soup.find_all()方法中获取漂亮汤python3中的文本?

如何从soup.find_all()方法中获取漂亮汤python3中的文本?
EN

Stack Overflow用户
提问于 2016-07-22 07:50:43
回答 1查看 4.7K关注 0票数 2

我想检索所有的<p>标记,然后用一个字符串从它们中获取文本。问题1.在运行这段代码时,它也给出了所有的标记。问题2.如果我使用loop打印con,它会提供多个字符串。如果我再用一根线把它附加起来,那就太笨拙了。那么,如何从下面的代码中得到简短的代码行呢?

代码语言:javascript
运行
复制
import re
import urllib.request 
from bs4 import BeautifulSoup


requ = urllib.request.Request("http://www.chowrangi.pk/10-reasons-gender-equality-not-exist.html", headers={'User-Agent': 'Chrome/51.0.2704.103'})
htmll = urllib.request.urlopen(requ).read()
soupi = BeautifulSoup(htmll,"html.parser")

Con= soupi.findAll("p")        
print(Con)

#  for con in soup.findAll("p"): 
#       print("Paragraph :" ,con.text)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-22 07:55:28

只需使用列表comp和str.join创建一个字符串:

代码语言:javascript
运行
复制
soup = BeautifulSoup(html, "html.parser")

Con = "".join([p.text for p in soup.find_all("p")])
print(Con)

这给了你:

代码语言:javascript
运行
复制
Women want equality, but they do not want to let go of their privileges. Women want freedom, but they do not want accountability. Women want to be liberated, but they do not want responsibility.Below are 15 reasons which shows how equality is only applicable when it works for women. Otherwise, women would be protesting against the following in order to achieve equality. It shows their hypocrisy and double standards. 1. Lifeboats are reserved for women.2. The media only focuses on women’s issues.3. World’s most dangerous jobs are worked by men.4. Seats are reserved for women on public transport.5. News channels announce deaths of ‘women’ and children.6. Juries discriminate against men in domestic violence disputes.7. Women have special quotas in the parliament, companies, and colleges.8. Women receive lighter sentences for the same crimes committed by men.9. Child custody is given to women is divorce courts, in the majority of cases.10. Men have to earn for women, but women are not under any obligation to earn for men.11. Domestic violence and dowry are seen as women’s issues, while men are the prime victims.12. Men give women child support and alimony, not the other way around. Men are ripped off their life savings.13. Men are used as ATMs. Women always marry men who are richer, earn more, ‘well-settled’, and better educated.14. Men die on jobs daily. 95% of work related deaths are of men, but that is neither an issue, not something that women and children are grateful for. 15. Draconian laws where women can land men behind bars with little evidence if any, giving a rise to false cases of dowry, rape, and domestic abuse. Police readily believe women, even though they lie more. Subscribe to our e-mail newsletter to receive updates.When blacks were forcrd to leave seats for whites, we called it slavery. When men are told to leave seats for women, we call it politeness! Why don’t women leave their seats for men?You need to add that women are released first in hostage situations, most teachers hired in schools are women, there is open discriminatory behaviour of teachers against boys in schools. men have to propose to women, women hitting men is socially acceptable, men make up the majority of homeless, war casualties, unemployed, suicides, compulsory conscription ….All the double standards are set by women.A man exposing in public = pervert
A woman exposing in public = liberatedMen seeking equal treatment = backlash
Women seeking equal treatment = feminismDiscrimination against men = equal opportunity
Discrimination against women = discriminationA woman with grievances = victim
A man with grievances = angryAny woman = victim
Any man = oppressorA woman talking about hating men = empowerment
A man talking about hating women = hate speechFemale genital mutilation = sexual repression
Male genital mutilation = acceptable customA man assaulting a women = violence
A woman assaulting a man = humorousA man who beats his female partner = batterer
A woman who beats her male partner = victimA disposable slave = man
A human being = womanHating women = a crime
Hating men = a viable political actAny power a man has = patriarchy
Any power a woman has = empowermentPornography pleasing to lesbians = erotica
Pornography pleasing to men = exploitation and degradation of womenPerson who say feminists are wrong = hate criminals
People who say men are wrong = feministsPatriarchy = bad
Matriarchy = goodMale leader = backwards
Female leader = improvementPro-lesbianism and female, anti-male = feminist ideology
Same standards, honest competition = unfairFemale virgin = pure
Male virgin = patheticFemale modesty = noble
Male modesty = creepyPandering to male audiences = sexism
Pandering to female audiences = fulfilling a nicheWomen standing up for themselves = empowerment
Men standing up for themselves = chauvinismWoman proud of her appearance = confident
Man proud of his appearance = vainInnate female advantages = complementary
Innate male advantages = sexistWomen’s space = safe haven
Men’s space = patriarchal breeding groundWomen discussing their issues = therapeutic
Men discussing their issues = whiningFemale intellect = pioneering
Male intellect = masturbatoryMan obeying a women = respect
Women obeying a man = slaveryMen being sexually critical = shallow
Women being sexually critical = having standardsFemale rage = man’s fault
Male rage = man’s faultMale abuse of power = direct consequence of patriarchy
Female abuse of power = indirect consequence of patriarchyUnemployed woman = homemaker
Unemployed man = loserFemale indulgence = success
Male indulgence = selfishnessComment Name (required) Email (will not be published) (required) Website 

© 2016 Chowrangi. All Rights Reserved. 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38521044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档