Python无法再检索1页的完整文本数据

2024-05-20 03:14:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python编程的新手,面临以下问题: 目标:我需要报废自由职业者网站和存储用户名单连同他们的属性(得分,评级,评论,细节,费率等) 变成一个文件。我有以下代码,但我不能得到所有的用户。你知道吗

而且,有时我运行程序时,输出会发生变化。你知道吗

import requests
from bs4 import BeautifulSoup

pages = 1
fileWriter =open('freelancers.txt','w')
url = 'https://www.freelancer.com/freelancers/skills/all/'+str(pages)+'/'
r = requests.get(url)

#gets the html contents and stores them into soup object

soup = BeautifulSoup(r.content)
links = soup.findAll("a")

#Finds the freelancer-details nodes and stores the html content into c_data

c_data = soup.findAll("div", {"class":"freelancer-details"})
for item in c_data:
    print item.text 
    fileWriter.write('Freelancers Details:'+item.text+'\t')
#Writes the result into text file

我需要获得特定用户下的用户详细信息。但到目前为止,产出似乎分散了。你知道吗

样本输出: 自由职业者详情:

thetechie13
507 Reviews




$20 USD/hr

Top Skills:

       Website Design, 


       HTML, 


       PHP, 


       eCommerce, 


       Volusion

Dear Customer - We are a team of 75 Most Creative People and proud to be
Preferred Freelancer on  Freelancer.com. We offer wide range of web
solutions and IT services that are bespoke in nature, can best fit our
clients' business needs and provide them cost benefits.

Tags: andthetext用户importdatapagesitem
1条回答
网友
1楼 · 发布于 2024-05-20 03:14:32

如果您希望每个单独的文本组件都独立(每个组件都指定了不同的名称),我建议您分别解析HTML中的文本。但是,如果要将所有字符串组合在一起,则可以将字符串连接起来:

print ' '.join(item.text.split())

这将在每个单词之间放置一个空格。你知道吗

相关问题 更多 >