一个很简单的爬虫问题,求大神解答

多啦A梦的时光机 2018-03-18 08:51:20
import requests as res
from bs4 import BeautifulSoup as bs
def body_function(url):
resp = res.get(url)
tex = resp.text
soup = bs(tex,'lxml')
status = resp.status_code
if status == 200:
return soup
def price_infor(html):
goods_jiage = html.find_all(class_='product-price')
for result_gj in goods_jiage:
ru1_1 = result_gj.find_all(class_='price-num')#获取卷后价格
for ru1 in ru1_1:
rus1 = ru1.get_text()
return rus1#用return返回结果时,无法返回所有结果,我知道print可以,但是无法拼接其他函数的返回结果
def baochou_infor(html):
goods_quan_info = html.find_all(class_='quan-infro')
for result_gqi in goods_quan_info:#获取佣金价格
ru2 = result_gqi.select('em')
ru2_1 = result_gqi.find('a')
rus2 = ru2_1['href']
n1 = list(ru2[0])
n2 = list(ru2[1])
ns = n1+n2
#print ('优惠卷:'+ns[0]+'元'+' 返现比例:'+ns[1]+'%'+' 优惠券链接:'+rus2)
def main():
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/64.0.3282.186 Safari/537.36'}
key = input("please input the keywords:")
page_num = int(input("input the pagenumber:"))
for pg in range(page_num):
url = 'http://www.taokezhushou.com/search?q='+key+'&page='+str(pg+1)
html = body_function(url)
a1 = price_infor(html)
print(a1)
#a2 = baochou_infor(html)
if __name__ == '__main__':
main()

用return返回结果,然后调取函数的时候得出全部的结果,我知道用print可以,用print输出结果会导致结果没办法和其他结果拼接在一起,就大神解答
爬取的网站是 http://www.taokezhushou.com
...全文
423 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ypgsh 2018-03-19
  • 打赏
  • 举报
回复
你在for循环里就直接return结果了,循环永远一轮就结束。应该用list先把结果存下来,循环结束后再return

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧