Aucune description

womanEveryday.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # encoding=utf8
  2. from scrapy.selector import Selector
  3. import scrapy
  4. import MySQLdb
  5. import time
  6. from pyquery import PyQuery as pq
  7. class quyaqu(scrapy.Spider):
  8. name="womanEveryday"
  9. start_urls=[
  10. 'http://m.39.net/woman/nxbj/',
  11. 'http://m.39.net/woman/nxqg/',
  12. 'http://m.39.net/woman/nxyy/',
  13. 'http://m.39.net/woman/nxht/',
  14. 'http://m.39.net/woman/nxaq/',
  15. 'http://m.39.net/woman/mrj/',
  16. 'http://m.39.net/woman/hwxz/',
  17. 'http://m.39.net/woman/jdsh/',
  18. 'http://m.39.net/woman/nxzx/',
  19. 'http://m.39.net/woman/nxsh/',
  20. 'http://m.39.net/woman/sms/',
  21. 'http://m.39.net/woman/xmt/',
  22. 'http://m.39.net/woman/zt/',
  23. 'http://m.39.net/woman/baike/nxby/'
  24. ]
  25. def parse(self, response):
  26. filename = response.url.split("/")[-2]
  27. # fhtml=open(filename, 'a')
  28. for quote in response.xpath("//li/a"):
  29. linkurl=(quote.xpath("./@href").extract_first())
  30. imgsrc=(quote.xpath("./img/@src").extract_first())
  31. spantitle=(quote.xpath("./span/text()").extract_first())
  32. if linkurl is not None:
  33. yield scrapy.Request(response.urljoin(linkurl),meta={'linkurl': linkurl,'imgsrc':imgsrc,'filename':filename,'spantitle':spantitle},callback=self.doparse)
  34. # fhtml.write(linkurl+ "\n")
  35. # fhtml.close()
  36. # next_page_url = response.xpath('//span[@class="nextPage"]/a/@href').extract_first()
  37. # if next_page_url is not None:
  38. # yield scrapy.Request(response.urljoin(next_page_url),callback=self.parse)
  39. def doparse(self,response):
  40. # filename = response.url.split("/")[-2]+str(self.index)
  41. # self.index=int(self.index)+1
  42. # with open(filename, 'ab') as f:
  43. # f.write(response.body)
  44. # f.close()
  45. filename = response.meta['filename']
  46. spantitle = response.meta['spantitle']
  47. linkurl = response.meta['linkurl']
  48. content=response.xpath('//div[@id="mArt_ps"]').extract_first()
  49. releaseTime=response.xpath('//div[@class="info w100 ov color3"]/span[1]/text()').extract_first()
  50. imgsrc=response.xpath('//div[@id="mArt_ps"]/p//img[1]/@src').extract_first()
  51. # print(releaseTime)
  52. # content=content.replace(u'\xa0', u' ')
  53. db = MySQLdb.connect("127.0.0.1","root","123456","ymgj",use_unicode=True,charset="utf8")
  54. cursor = db.cursor()
  55. # sql = "INSERT INTO news(title,imgUrl,linkUrl,content,createTime,category) VALUES ('%s', '%s','%s','%s','%s','%s')"%(spantitle,imgsrc,linkurl,content,releaseTime,filename)
  56. # try:
  57. # # # 执行sql语句
  58. # cursor.execute(sql)
  59. # # 提交到数据库执行
  60. # db.commit()
  61. # except:
  62. # # # Rollback in case there is any error
  63. # db.rollback()
  64. sqlone = "SELECT * FROM news WHERE linkUrl = '%s'" % (linkurl);
  65. try:
  66. # 执行SQL语句
  67. cursor.execute(sqlone)
  68. # 获取所有记录列表
  69. results=cursor.fetchall()
  70. if(len(results)==0):
  71. sql = "INSERT INTO news(title,imgUrl,linkUrl,content,createTime,category) VALUES ('%s', '%s','%s','%s','%s','%s')"%(spantitle,imgsrc,linkurl,content,releaseTime,filename)
  72. try:
  73. # # 执行sql语句
  74. cursor.execute(sql)
  75. # 提交到数据库执行
  76. db.commit()
  77. except:
  78. # # Rollback in case there is any error
  79. db.rollback()
  80. except:
  81. print(1);
  82. # 关闭数据库连接
  83. db.close()
  84. # for quotein in response.xpath("//li/a"):
  85. # sel = Selector(response)
  86. # sites = sel.xpath('//img')
  87. # for site in sites:
  88. # title = site.xpath('@src').extract()
  89. # print(title)
  90. # sel = Selector(response)
  91. # sites = sel.xpath('//li/a')
  92. # filename = response.url.split("/")[-2]
  93. # fhtml=open(filename, 'a')
  94. # for site in sites:
  95. # title = site.xpath('@src').extract()
  96. # fhtml.write("".join(title)+ "\n")
  97. # fhtml.close()