我在ubuntu 14.04上运行python 2.7.6!
我设计了一个基本的网络抓取工具,它应该输出页面上的所有链接和相应的标题。 但它没有给出任何结果。
#!usr/bin/python3
from bs4 import BeautifulSoup
import requests
def web_crawl(url):
source_code=requests.get(url)
plain_text=source_code.text
soup=BeautifulSoup(plain_text)
for link in soup.findAll('a', {'class':"pu-image fk-product-thumb" }):
href="http://www.flipkart.com" + link.get('href')
print(href)
title=link.string
print(title)
web_crawl('http://www.flipkart.com/laptops/pr?p%5B%5D=facets.availability%255B%255D%3DExclude%2BOut%2Bof%2BStock&p%5B%5D=sort%3Dpopularity&sid=6bo%2Cb5g&otracker=clp_computers-laptops_CategoryLinksModule_0-2_catergorylinks_0_AllBrands')
它只是运行并且没有输出! 有人可以帮忙吗?
答案 0 :(得分:1)
所有你需要的是在课程结束时的空白区域'属性的价值。
from bs4 import BeautifulSoup
import requests
def web_crawl(url):
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, 'lxml')
for link in soup.findAll('a', {'class': 'pu-image fk-product-thumb '}):
href = "http://www.flipkart.com" + link.get('href')
print(href)
title = link.string
print(title)
web_crawl(
'http://www.flipkart.com/laptops/pr?p%5B%5D=facets.availability%255B%255D%3DExclude%2BOut%2Bof%2BStock&p%5B%5D=sort%3Dpopularity&sid=6bo%2Cb5g&otracker=clp_computers-laptops_CategoryLinksModule_0-2_catergorylinks_0_AllBrands')
输出 -
http://www.flipkart.com/hp-15-af114au-p3c92pa-acj-apu-quad-core-a8-4-gb-ddr3-1-tb-hdd-windows-10-notebook/p/itmeae7xhwzk9auy?pid=COMEAE7XDPMSAMRW&al=KzWJLVkIvPpZDdESq3R4%2BsldugMWZuE75aUsiwTbcEN7yrs5t0ztNSrlGlv2diDGVwslAzv5HNU%3D&ref=L%3A-781049773755580266&srno=b_1
None
http://www.flipkart.com/hp-ac184tu-ac-series-t0x61pa-intel-core-i3-5th-gen-4-gb-ddr3-1-tb-hdd-free-dos-128-mb-graphics-notebook/p/itmecfmt7wyeumbc?pid=COMECFMT9VYMAPQ7&al=KzWJLVkIvPpZDdESq3R4%2BsldugMWZuE75aUsiwTbcEPRhgJOx%2FdUHSMbdm%2FRcmHwgNHMThXL6sc%3D&ref=L%3A-781049773755580266&srno=b_2
None
http://www.flipkart.com/lenovo-g50-g-series-y700-15isk-80e3020bih-apu-quad-core-a8-6th-gen-4-gb-ddr3-1-tb-hdd-windows-10-notebook/p/itmefggpvhxm5wzt?pid=COMEFGGPGSQRMSY3&al=KzWJLVkIvPpZDdESq3R4%2BsldugMWZuE75aUsiwTbcEPFx40k%2Fvi8TCf5fcQVGrdBfmYsaOLhdKU%3D&ref=L%3A-781049773755580266&srno=b_3
None
http://www.flipkart.com/hp-15-ac170tu-p6l83pa-acj-core-i3-5th-gen-4-gb-ddr3-500-hdd-free-dos-notebook/p/itmebbdzhc3npfhg?pid=COMEBBDZUTTFJ78F&al=KzWJLVkIvPpZDdESq3R4%2BsldugMWZuE75aUsiwTbcEMkQT%2BGnwV9%2FQw37CzdKqKMLLLvhttzbIU%3D&ref=L%3A-781049773755580266&srno=b_4
None
http://www.flipkart.com/lenovo-ideapad-100-14ibd-80rk002uih-core-i3-5th-gen-4-gb-ddr3-500-hdd-windows-10-notebook/p/itmefqk9hf98zmgz?pid=COMEFQK9PPTWZVNJ&al=KzWJLVkIvPpZDdESq3R4%2BsldugMWZuE75aUsiwTbcEMw%2BN%2FjkKRhQgo9jJ9LA5Nz1JTCNlH7C4Y%3D&ref=L%3A-781049773755580266&srno=b_5
None
http://www.flipkart.com/dell-inspiron-15-3541-3541a64500ibu-apu-quad-core-a6-4-gb-ddr3-500-hdd-ubuntu-notebook/p/itmeatzfmy6ydtyt?pid=COMEATZFX9QYGZKY&al=KzWJLVkIvPpZDdESq3R4%2BsldugMWZuE75aUsiwTbcEMA99D%2BTaAwtVj3Png4RpQL5jcnG1ucjc0%3D&ref=L%3A-781049773755580266&srno=b_6
None
http://www.flipkart.com/lenovo-g50-80-80e503cmih-core-i5-5th-gen-8-gb-ddr3-1-tb-hdd-free-dos-2-graphics-notebook/p/itmegwzhnb7nwkgz?pid=COMEGWZKSHXREAHH&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcEO7Pjd%2BV6XLl%2FssLvCIbuS1v20DVax%2BCEc%3D&ref=L%3A-781049773755580266&srno=b_7
None
http://www.flipkart.com/lenovo-g50-80-80e5038pin-intel-core-i5-5th-gen-8-gb-ddr3-1-tb-hdd-windows-10-2-graphics-notebook/p/itmebh7v5c2fsjby?pid=COMEBH7VFP2HFXGU&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcENrBd4C6qBVbUS5ZGcfdoQGJdHm8ssXnDA%3D&ref=L%3A-781049773755580266&srno=b_8
None
http://www.flipkart.com/acer-one-10-s1001-19p0-nt-g86si-002-intel-atom-quad-core-2-gb-ddr3-32-emmc-hdd-windows-10-2-1-laptop/p/itmedgthjrymga9v?pid=COMEDGTK55C8BRXY&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcEPz5ZfQuCtNb2WC81UuX7%2FEXvhPNfjAKSY%3D&ref=L%3A-781049773755580266&srno=b_9
None
http://www.flipkart.com/acer-aspire-e-e5-573g-389u-nx-mvmsi-036-core-i3-5th-gen-8-gb-ddr3-1-tb-hdd-windows-10-2-graphics-notebook/p/itmeck24uz59c8m6?pid=COMECK24KZW4GZWG&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcEOmkGzEuHsI6aTkUnwJVmijLoX1DXNTnbY%3D&ref=L%3A-781049773755580266&srno=b_10
None
http://www.flipkart.com/hp-15-ac122tu-ac-c04779457-n8m18pa-core-i3-5th-gen-4-gb-ddr3-1-tb-hdd-free-dos-128-mb-graphics-notebook/p/itmeb9zgczhv6pjk?pid=COMEB9ZGRKFE5JG4&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcEM%2FIQetobkSyTwvHeQCM2tJ4kGPWzdzJhs%3D&ref=L%3A-781049773755580266&srno=b_11
None
http://www.flipkart.com/hp-15-ac149tx-p6l84pa-acj-core-i3-5th-gen-8-gb-ddr3-1-tb-hdd-free-dos-2-graphics-notebook/p/itmedcvk7pbpgyat?pid=COMEDCVKGGSKXZB3&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcEPVfnzI7h5SVCHLk8DlSBpCHU2GZPpz0Pk%3D&ref=L%3A-781049773755580266&srno=b_12
None
http://www.flipkart.com/dell-inspiron-3542-354234500ibu-core-i3-4-gb-ddr3-500-hdd-linux-ubuntu-notebook/p/itmdz5pwkzkmafrn?pid=COMDZ5MYYJ9DAFHT&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcEMwvDvSb%2FGK8M8ZY5BA50RxCaqen7Xb0Cg%3D&ref=L%3A-781049773755580266&srno=b_13
None
http://www.flipkart.com/hp-pavilion-11-s003tu-w0h99pa-celeron-dual-core-2-gb-ddr3-500-hdd-free-dos-notebook/p/itmeg4z7rqzkk5vc?pid=COMEG4Z74KYAHTDC&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcEOmHHGyc4GKlcQaMy%2B%2BSIgFVLAMShFMM4s%3D&ref=L%3A-781049773755580266&srno=b_14
None
http://www.flipkart.com/acer-one-14-z1402-394d-nx-g80si-012-intel-core-i3-5th-gen-4-gb-ddr3-500-hdd-windows-10-notebook/p/itmebepuufkknt2w?pid=COMEBEPUQHWBVYHH&al=KzWJLVkIvPpms%2FA%2FGj2ELsldugMWZuE75aUsiwTbcENHdQYSTku2cHjj%2F1suFC5AmJwefTorgYU%3D&ref=L%3A-781049773755580266&srno=b_15
None
http://www.flipkart.com/lenovo-g50-45-80e301n3in-apu-quad-core-a8-8-gb-ddr3-1-tb-hdd-free-dos-2-graphics-notebook/p/itmeeb2nz832qc7j?pid=COMEEB2N5NCQDPMF&al=KzWJLVkIvPqNOPvRIiseqcldugMWZuE75aUsiwTbcEPqqkTaKz5%2BDYM2d8dhiX5%2BmS%2FB2M6SxIE%3D&ref=L%3A-781049773755580266&srno=b_17
None
http://www.flipkart.com/hp-15-ac650tu-v5d75pa-core-i5-4th-gen-4-gb-ddr3-1-tb-hdd-free-dos-notebook/p/itmegh45b7uuur4t?pid=COMEGH45ZYFHZ7NZ&al=KzWJLVkIvPqNOPvRIiseqcldugMWZuE75aUsiwTbcEN4vpFSnt9vilFAyX9cF5%2FxkRVNXBASCLg%3D&ref=L%3A-781049773755580266&srno=b_18
None
http://www.flipkart.com/hp-15-ac152tx-p6l87pa-acj-intel-core-i5-6th-gen-8-gb-ddr3-1-tb-hdd-free-dos-2-graphics-notebook/p/itmeeffdjaxwvdjz?pid=COMEEFFDM4YTFUSH&al=KzWJLVkIvPqNOPvRIiseqcldugMWZuE75aUsiwTbcENKPVBr2Jwv1gApMSp9G%2FB%2BhM7ZG4%2F2SDo%3D&ref=L%3A-781049773755580266&srno=b_19
None
http://www.flipkart.com/hp-pavilion13-x360-s102tu-t0y58pa-acj-core-i3-6th-gen-4-gb-ddr3-1-tb-hdd-windows-10-notebook/p/itmeck4gmy3uqejg?pid=COMECK4GHCWHXF3F&al=KzWJLVkIvPqNOPvRIiseqcldugMWZuE75aUsiwTbcEMStgqKA3RJW8Gnq5zrf2V%2BZr6N634tjz0%3D&ref=L%3A-781049773755580266&srno=b_20
None